Test markdown image lazy loading extensions
This commit is contained in:
parent
b30901af69
commit
9930d00f7a
3 changed files with 19 additions and 3 deletions
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Reference in a new issue