2022-09-30 17:33:13 +08:00
|
|
|
import fs from 'fs-extra';
|
2022-10-28 22:07:12 +08:00
|
|
|
import path from 'path';
|
2022-09-30 17:33:13 +08:00
|
|
|
import rehypeParse from 'rehype-parse';
|
2022-10-29 00:24:39 +08:00
|
|
|
import { unified } from 'unified';
|
2022-09-30 17:33:13 +08:00
|
|
|
import { VFile } from 'vfile';
|
|
|
|
|
2022-10-29 00:24:39 +08:00
|
|
|
export const ICONS_PATH = path.resolve(process.cwd(), 'scripts/assets');
|
2022-10-28 22:07:12 +08:00
|
|
|
|
2022-10-23 22:27:07 +08:00
|
|
|
export function getSVGNode(iconPath, space = 'svg') {
|
2022-09-30 17:33:13 +08:00
|
|
|
const svgStr = fs.readFileSync(iconPath);
|
2022-10-29 00:24:39 +08:00
|
|
|
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);
|
2022-10-29 00:24:39 +08:00
|
|
|
return hastNode.children || [];
|
2022-10-23 22:27:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getVNode(str = '', space = 'html') {
|
2022-10-29 00:24:39 +08:00
|
|
|
const processor = unified().use(rehypeParse, { fragment: true, space });
|
2022-10-23 22:27:07 +08:00
|
|
|
const file = new VFile();
|
|
|
|
file.value = str.toString();
|
|
|
|
const hastNode = processor.runSync(processor.parse(file), file);
|
2022-10-29 00:24:39 +08:00
|
|
|
return hastNode.children || [];
|
|
|
|
}
|