Test markdown image lazy loading extensions

This commit is contained in:
Gabriel Augendre 2020-12-03 21:50:53 +01:00
parent b30901af69
commit 9930d00f7a
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
3 changed files with 19 additions and 3 deletions

View file

@ -11,14 +11,16 @@ from markdown.inlinepatterns import (
class LazyImageInlineProcessor(ImageInlineProcessor):
def handleMatch(self, m, data):
el, match_start, index = super().handleMatch(m, data)
el.set("loading", "lazy")
if el is not None:
el.set("loading", "lazy")
return el, match_start, index
class LazyImageReferenceInlineProcessor(ImageReferenceInlineProcessor):
def makeTag(self, href, title, text):
el = super().makeTag(href, title, text)
el.set("loading", "lazy")
if el is not None:
el.set("loading", "lazy")
return el

View file

@ -19,7 +19,13 @@ def published_article(author: User) -> Article:
author=author,
published_at=timezone.now(),
slug="some-article-slug",
content="## some article markdown\n\n[an article link](https://article.com)",
content=(
"## some article markdown\n\n"
"[an article link](https://article.com)\n"
"![an image](https://article.com)\n"
"![a referenced image][1]\n\n"
"[1]: https://example.com/image.png"
),
)

View file

@ -117,3 +117,11 @@ def test_logged_in_user_doesnt_have_plausible(client: Client, author: User, sett
res = client.get(reverse("articles-list"))
content = res.content.decode("utf-8")
assert "https://plausible.augendre.info/js/plausible.js" not in content
@pytest.mark.django_db
def test_image_is_lazy(client: Client, published_article: Article):
res = client.get(reverse("article-detail", kwargs={"slug": published_article.slug}))
assert res.status_code == 200
content = res.content.decode("utf-8")
assert content.count('loading="lazy"') == 2