-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
37 lines (27 loc) · 899 Bytes
/
index.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
28
29
30
31
32
33
34
35
36
37
import path from 'node:path';
import { visit } from 'unist-util-visit';
import transform from './transform.js';
export default (options = {}) => {
const suffix = options.suffix || '.inline.svg';
return async (tree, file) => {
const svgs = [];
const markdownFileDir = path.dirname(file.history[0]);
visit(tree, 'image', (node) => {
const { url } = node;
if (url.endsWith(suffix)) {
// this makes it work when SVGs are prefixed with a /, like /img/circle.svg
const fullPath = url.replace(/^\/+/, process.cwd() + '/');
node.url = path.resolve('./', markdownFileDir, fullPath);
svgs.push(node);
}
});
if (svgs.length > 0) {
const promises = svgs.map(async (node) => {
return await transform(node, options);
});
await Promise.all(promises);
}
return tree;
};
};
export { transform };