forked from getzola/zola
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for lazy loading images (getzola#2211)
* Add optional decoding="async" loading="lazy" for img In theory, they can make the page load faster and show content faster. There’s one problem: CommonMark allows arbitrary inline elements in alt text. If I want to get the correct alt text, I need to match every inline event. I think most people will only use plain text, so I only match Event::Text. * Add very basic test for img This is the reason why we should use plain text when lazy_async_image is enabled. * Explain lazy_async_image in documentation * Add test with empty alt and special characters I totaly forgot one can leave the alt text empty. I thought I need to eliminate the alt attribute in that case, but actually empty alt text is better than not having an alt attribute at all: https://www.w3.org/TR/WCAG20-TECHS/H67.html https://www.boia.org/blog/images-that-dont-need-alternative-text-still-need-alt-attributes Thus I will leave the empty alt text. Another test is added to ensure alt text is properly escaped. I will remove the redundant escaping code after this commit. * Remove manually escaping alt text After removing the if-else inside the arm of Event::Text(text), the alt text is still escaped. Indeed they are redundant. * Use insta for snapshot testing `cargo insta review` looks cool! I wanted to dedup the cases variable, but my Rust skill is not good enough to declare a global vector.
- Loading branch information
1 parent
b9860c8
commit b2a355e
Showing
6 changed files
with
89 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
mod common; | ||
use config::Config; | ||
|
||
#[test] | ||
fn can_transform_image() { | ||
let cases = vec![ | ||
"![haha](https://example.com/abc.jpg)", | ||
"![](https://example.com/abc.jpg)", | ||
"![ha\"h>a](https://example.com/abc.jpg)", | ||
"![__ha__*ha*](https://example.com/abc.jpg)", | ||
"![ha[ha](https://example.com)](https://example.com/abc.jpg)", | ||
]; | ||
|
||
let body = common::render(&cases.join("\n")).unwrap().body; | ||
insta::assert_snapshot!(body); | ||
} | ||
|
||
#[test] | ||
fn can_add_lazy_loading_and_async_decoding() { | ||
let cases = vec![ | ||
"![haha](https://example.com/abc.jpg)", | ||
"![](https://example.com/abc.jpg)", | ||
"![ha\"h>a](https://example.com/abc.jpg)", | ||
"![__ha__*ha*](https://example.com/abc.jpg)", | ||
"![ha[ha](https://example.com)](https://example.com/abc.jpg)", | ||
]; | ||
|
||
let mut config = Config::default_for_test(); | ||
config.markdown.lazy_async_image = true; | ||
|
||
let body = common::render_with_config(&cases.join("\n"), config).unwrap().body; | ||
insta::assert_snapshot!(body); | ||
} |
10 changes: 10 additions & 0 deletions
10
components/markdown/tests/snapshots/img__can_add_lazy_loading_and_async_decoding.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
source: components/markdown/tests/img.rs | ||
expression: body | ||
--- | ||
<p><img src="https://example.com/abc.jpg" alt="haha" loading="lazy" decoding="async" /> | ||
<img src="https://example.com/abc.jpg" alt="" loading="lazy" decoding="async" /> | ||
<img src="https://example.com/abc.jpg" alt="ha"h>a" loading="lazy" decoding="async" /> | ||
<img src="https://example.com/abc.jpg" alt="<strong>ha</strong><em>ha</em>" loading="lazy" decoding="async" /> | ||
<img src="https://example.com/abc.jpg" alt="ha<a href="https://example.com">ha</a>" loading="lazy" decoding="async" /></p> | ||
|
10 changes: 10 additions & 0 deletions
10
components/markdown/tests/snapshots/img__can_transform_image.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
source: components/markdown/tests/img.rs | ||
expression: body | ||
--- | ||
<p><img src="https://example.com/abc.jpg" alt="haha" /> | ||
<img src="https://example.com/abc.jpg" alt="" /> | ||
<img src="https://example.com/abc.jpg" alt="ha"h>a" /> | ||
<img src="https://example.com/abc.jpg" alt="haha" /> | ||
<img src="https://example.com/abc.jpg" alt="haha" /></p> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters