-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform.js
50 lines (39 loc) · 1.11 KB
/
transform.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
38
39
40
41
42
43
44
45
46
47
48
49
50
import fs from 'node:fs/promises';
import { optimize } from 'svgo';
const svgoPlugins = [
{
name: 'preset-default',
params: {
overrides: {
// disable plugins
removeTitle: false,
removeDesc: false,
},
},
},
'removeXMLNS',
'removeDimensions',
];
const inlineSVG = async (node, options) => {
const { alt, url } = node;
const image = await fs.readFile(url, 'utf-8');
const result = await optimize(image, { plugins: svgoPlugins });
let { data: html } = result;
if (options.replace) {
Object.entries(options.replace).forEach(([str, replacement]) => {
const re = new RegExp(str, 'g');
if (!!html === false) {
console.log(`[remark-inline-svg]: problem optimizing ${url}`);
}
html = html.replace(re, replacement);
});
}
const [, svgAttrs] = html.match(/<svg (.*?)>/);
html = html.replace(svgAttrs, `${svgAttrs} role="img" aria-hidden="true"`);
const className = options.className || 'markdown-inline-svg';
node.type = 'html';
node.value = `<figure class="${className}">
${html}
</figure>`;
};
export default inlineSVG;