-
Notifications
You must be signed in to change notification settings - Fork 972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for lazy loading images #2211
Changes from 5 commits
06e9a40
6127764
dd45fb3
bd85ca4
e7ae995
b351471
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
mod common; | ||
use config::Config; | ||
|
||
#[test] | ||
fn can_transform_image() { | ||
// normal image | ||
let rendered = common::render("![haha](https://example.com/abc.jpg)").unwrap().body; | ||
assert_eq!(rendered, "<p><img src=\"https://example.com/abc.jpg\" alt=\"haha\" /></p>\n"); | ||
|
||
// empty alt text | ||
let rendered = common::render("![](https://example.com/abc.jpg)").unwrap().body; | ||
assert_eq!(rendered, "<p><img src=\"https://example.com/abc.jpg\" alt=\"\" /></p>\n"); | ||
|
||
// alt text needs to be escaped | ||
let rendered = common::render("![ha\"h>a](https://example.com/abc.jpg)").unwrap().body; | ||
assert_eq!(rendered, "<p><img src=\"https://example.com/abc.jpg\" alt=\"ha"h>a\" /></p>\n"); | ||
|
||
// alt text with style | ||
let rendered = common::render("![__ha__*ha*](https://example.com/abc.jpg)").unwrap().body; | ||
assert_eq!(rendered, "<p><img src=\"https://example.com/abc.jpg\" alt=\"haha\" /></p>\n"); | ||
|
||
// alt text with link | ||
let rendered = | ||
common::render("![ha[ha](https://example.com)](https://example.com/abc.jpg)").unwrap().body; | ||
assert_eq!(rendered, "<p><img src=\"https://example.com/abc.jpg\" alt=\"haha\" /></p>\n"); | ||
} | ||
|
||
#[test] | ||
fn can_add_lazy_loading_and_async_decoding() { | ||
let mut config = Config::default_for_test(); | ||
config.markdown.lazy_async_image = true; | ||
|
||
// normal alt text | ||
let rendered = | ||
common::render_with_config("![haha](https://example.com/abc.jpg)", config.clone()) | ||
.unwrap() | ||
.body; | ||
assert_eq!(rendered, "<p><img src=\"https://example.com/abc.jpg\" alt=\"haha\" loading=\"lazy\" decoding=\"async\" /></p>\n"); | ||
|
||
// empty alt text | ||
let rendered = common::render_with_config("![](https://example.com/abc.jpg)", config.clone()) | ||
.unwrap() | ||
.body; | ||
assert_eq!(rendered, "<p><img src=\"https://example.com/abc.jpg\" alt=\"\" loading=\"lazy\" decoding=\"async\" /></p>\n"); | ||
|
||
// alt text needs to be escaped | ||
let rendered = | ||
common::render_with_config("![ha\"h>a](https://example.com/abc.jpg)", config.clone()) | ||
.unwrap() | ||
.body; | ||
assert_eq!(rendered, "<p><img src=\"https://example.com/abc.jpg\" alt=\"ha"h>a\" loading=\"lazy\" decoding=\"async\" /></p>\n"); | ||
|
||
// Below is acceptable, but not recommended by CommonMark | ||
|
||
// alt text with style | ||
let rendered = | ||
common::render_with_config("![__ha__*ha*](https://example.com/abc.jpg)", config.clone()) | ||
.unwrap() | ||
.body; | ||
assert_eq!(rendered, "<p><img src=\"https://example.com/abc.jpg\" alt=\"<strong>ha</strong><em>ha</em>\" loading=\"lazy\" decoding=\"async\" /></p>\n"); | ||
|
||
// alt text with link | ||
let rendered = common::render_with_config( | ||
"![ha[ha](https://example.com)](https://example.com/abc.jpg)", | ||
config.clone(), | ||
) | ||
.unwrap() | ||
.body; | ||
assert_eq!(rendered, "<p><img src=\"https://example.com/abc.jpg\" alt=\"ha<a href=\"https://example.com\">ha</a>\" loading=\"lazy\" decoding=\"async\" /></p>\n"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test with an empty alt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! I added it for both pulldown-cmark and mine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I forgot zola was already using insta for snapshot testing. Can you check eg There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No worries, I want to try it, so I’ve converted the test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not certain there will be an alt right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the user might not input any alt text, but we still need to emit an empty alt attribute for the screen reader to ignore the picture: https://www.boia.org/blog/images-that-dont-need-alternative-text-still-need-alt-attributes