markdown-it-image-lazy-loading
Advanced tools
Comparing version 1.0.2 to 1.1.0
15
index.js
'use strict'; | ||
module.exports = function lazy_loading_plugin(md, options) { | ||
module.exports = function lazy_loading_plugin(md, mdOptions) { | ||
var defaultImageRenderer = md.renderer.rules.image; | ||
@@ -9,4 +9,17 @@ | ||
token.attrSet('loading', 'lazy'); | ||
if(mdOptions && mdOptions.base_path && mdOptions.image_size === true){ | ||
const sizeOf = require('image-size'); | ||
const path = require('path'); | ||
const imgSrc = token.attrGet('src'); | ||
const imgPath = path.join(mdOptions.base_path, imgSrc); | ||
const dimensions = sizeOf(imgPath); | ||
token.attrSet('width', dimensions.width); | ||
token.attrSet('height', dimensions.height); | ||
} | ||
return defaultImageRenderer(tokens, idx, options, env, self); | ||
}; | ||
}; |
{ | ||
"name": "markdown-it-image-lazy-loading", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "a markdown-it plugin supporting Chrome 75's native image lazy-loading", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node testing.js" | ||
"test": "npx tape test/*.js" | ||
}, | ||
@@ -18,6 +18,9 @@ "keywords": [ | ||
"license": "MIT", | ||
"dependencies": { | ||
"image-size": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"markdown-it": "^9.1.0", | ||
"tape": "^4.11.0" | ||
"markdown-it": "12.x", | ||
"tape": "5.x" | ||
} | ||
} |
@@ -20,4 +20,26 @@ A markdown-it plugin supporting Chrome 75's [native image lazy-loading](https://addyosmani.com/blog/lazy-loading/). | ||
The plugin can also add `width` and `height` attributes to each image. This can prevent [cumulative layout shifts (CLS)](https://web.dev/cls/): | ||
```javascript | ||
md.use(lazy_loading, { | ||
image_size: true, | ||
// Where your images are stored | ||
base_path: __dirname + 'src/', | ||
}); | ||
md.render(`![](example.png "image title")`); | ||
// <p><img src="example.png" alt="" title="image title" loading="lazy" width="100" height="100"></p>\n | ||
``` | ||
To keep images responsive, also include the following CSS: | ||
```css | ||
img{ | ||
max-width: 100%; | ||
height: auto; | ||
} | ||
``` | ||
## License | ||
MIT |
2452
45
1
3
18
+ Addedimage-size@^1.0.0
+ Addedimage-size@1.1.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedqueue@6.0.2(transitive)