-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathremark-image-size.js
27 lines (25 loc) · 1006 Bytes
/
remark-image-size.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import path from 'path';
import getImageSize from 'image-size';
import {visit} from 'unist-util-visit';
/**
* appends the image size to the image url as a hash e.g. /img.png -> /img.png#100x100
* the size is consumed by docImage.tsx and passed down to next/image
* **this is a hack!**, there's probably a better way to set image node properties
* but adding a hash to the url seems like a very low risk way to do it 🙈
*/
export default function remarkImageSize(options) {
return tree =>
visit(tree, 'image', node => {
// don't process external images
if (node.url.startsWith('http')) {
return;
}
const fullImagePath = path.join(
// if the path starts with / it's a public asset, otherwise it's a relative path
node.url.startsWith('/') ? options.publicFolder : options.sourceFolder,
node.url
);
const imageSize = getImageSize(fullImagePath);
node.url = node.url + `#${imageSize.width}x${imageSize.height}`;
});
}