mirror of
https://github.com/getzola/zola
synced 2024-12-14 06:12:27 +00:00
22dc32a589
* 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.
33 lines
1 KiB
Rust
33 lines
1 KiB
Rust
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);
|
|
}
|