add loading=lazy
to images by default
#2441
jimmywarting
started this conversation in
Ideas
Replies: 2 comments
-
You could create and extension for that: // marked-loading-lazy.js
export const loadingLazy = {
name: 'image',
level: 'inline',
renderer(token) {
const html = this.parser.renderer.image(token.href, token.title, token.text);
return html.replace(/^<img /, '<img loading="lazy" ');
},
}; import {marked} from 'marked';
import {loadingLazy} from './marked-loading-lazy.js';
marked.use({extensions: [loadingLazy]});
console.log(marked(''));
// <p><img loading="lazy" src="test.png" alt="test"></p> If you wanted to you could create an npm package so anyone who wanted it could use it. |
Beta Was this translation helpful? Give feedback.
0 replies
-
As said in the documentation (https://marked.js.org/using_pro#renderer), you can define renderers for different html elements, including images, it looks like this: marked.use({
your options,
renderer: {
image(href, title, text) {
return `<img loading="lazy" alt="${text}" title="${title}" src="${href}">`
}
}
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
It would have been nice if images added the lazy loading technic on images.
https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading
example input:
Example output:
Beta Was this translation helpful? Give feedback.
All reactions