Skip to content

Commit 8991293

Browse files
committed
feat: add ESM support
1 parent 6d8a304 commit 8991293

File tree

7 files changed

+49
-9
lines changed

7 files changed

+49
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
2+
index.js
23
package-lock.json
34

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ $ npm install markdown-it-image-lazy-loading
88

99
## Usage
1010

11+
Load it in ES module.
12+
13+
```javascript
14+
import markdownit from 'markdown-it';
15+
import lazy_loading from 'markdown-it-lazy-loading';
16+
17+
const md = markdownit();
18+
md.use(lazy_loading);
19+
md.render(`![](example.png "image title")`);
20+
// <p><img src="example.png" alt="" title="image title" loading="lazy"></p>\n
21+
```
22+
23+
Or load it in CommonJS module.
24+
1125
```javascript
1226
const md = require('markdown-it')();
1327
const lazy_loading = require('markdown-it-image-lazy-loading');
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
'use strict';
1+
import imageSize from 'image-size';
2+
import path from 'node:path';
23

3-
module.exports = function lazy_loading_plugin(md, mdOptions) {
4+
function lazy_loading_plugin(md, mdOptions) {
45
var defaultImageRenderer = md.renderer.rules.image;
56

67
md.renderer.rules.image = function (tokens, idx, options, env, self) {
@@ -12,12 +13,9 @@ module.exports = function lazy_loading_plugin(md, mdOptions) {
1213
}
1314

1415
if (mdOptions && mdOptions.base_path && mdOptions.image_size === true) {
15-
const sizeOf = require('image-size');
16-
const path = require('path');
17-
1816
const imgSrc = token.attrGet('src');
1917
const imgPath = path.join(mdOptions.base_path, imgSrc);
20-
const dimensions = sizeOf(imgPath);
18+
const dimensions = imageSize(imgPath);
2119

2220
token.attrSet('width', dimensions.width);
2321
token.attrSet('height', dimensions.height);
@@ -26,3 +24,6 @@ module.exports = function lazy_loading_plugin(md, mdOptions) {
2624
return defaultImageRenderer(tokens, idx, options, env, self);
2725
};
2826
};
27+
28+
export default lazy_loading_plugin;
29+

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
"version": "1.2.0",
44
"description": "a markdown-it plugin supporting Chrome 75's native image lazy-loading",
55
"main": "index.js",
6+
"exports": {
7+
".": {
8+
"import": "./index.mjs",
9+
"require": "./index.js"
10+
}
11+
},
612
"scripts": {
7-
"test": "npx tape test/*.js"
13+
"build": "rollup --config rollup.config.mjs",
14+
"test": "npm run build && npx tape test/*.js",
15+
"prepublishOnly": "npm run build"
816
},
917
"keywords": [
1018
"markdown-it-plugin",
@@ -19,7 +27,8 @@
1927
"image-size": "^1.0.0"
2028
},
2129
"devDependencies": {
22-
"markdown-it": "^12.3.0",
30+
"markdown-it": "^14.0.0",
31+
"rollup": "^4.9.1",
2332
"tape": "^5.3.2"
2433
}
2534
}

rollup.config.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
input: 'index.mjs',
3+
output: {
4+
file: 'index.js',
5+
format: 'cjs'
6+
},
7+
external: ['image-size', 'node:path']
8+
};

test.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import markdownit from 'markdown-it';
2+
import lazy_loading from './index.mjs';
3+
4+
const md = markdownit();
5+
6+
md.use(lazy_loading);
7+
console.log(md.render(`![](example.png "image title")`));
8+
// <p><img src="example.png" alt="" title="image title" loading="lazy"></p>\n

test/basic.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ test('lazy loading test', function (t) {
1313
);
1414

1515
});
16-

0 commit comments

Comments
 (0)