reference/scripts/utils/getSVGNode.mjs

21 lines
754 B
JavaScript
Raw Normal View History

2022-09-30 17:33:13 +08:00
import fs from 'fs-extra';
import rehypeParse from 'rehype-parse';
import {unified} from 'unified';
import { VFile } from 'vfile';
export function getSVGNode(iconPath, space = 'svg') {
2022-09-30 17:33:13 +08:00
const svgStr = fs.readFileSync(iconPath);
const processor = unified().use(rehypeParse,{ fragment: true, space })
2022-09-30 17:33:13 +08:00
const file = new VFile();
file.value = svgStr.toString();
const hastNode = processor.runSync(processor.parse(file), file);
return hastNode.children || []
}
export function getVNode(str = '', space = 'html') {
const processor = unified().use(rehypeParse,{ fragment: true, space })
const file = new VFile();
file.value = str.toString();
const hastNode = processor.runSync(processor.parse(file), file);
return hastNode.children || []
2022-09-30 17:33:13 +08:00
}