reference/scripts/utils/rehypeTitle.mjs

21 lines
731 B
JavaScript
Raw Normal View History

2022-10-15 13:32:12 +08:00
import fs from 'fs-extra';
import path from 'path';
import { getSVGNode, ICONS_PATH } from './getSVGNode.mjs';
2022-10-15 13:32:12 +08:00
export function rehypeTitle(node, iconName) {
if (node.type === 'element' && node.tagName === 'h1' && iconName !== 'index') {
const iconPath = path.resolve(ICONS_PATH, `${iconName}.svg`);
const iconDefaultPath = path.resolve(ICONS_PATH, `list.svg`);
const iconExist = fs.existsSync(iconPath);
if (iconExist) {
const svgNode = getSVGNode(iconPath);
2022-10-29 00:24:39 +08:00
node.children = [...svgNode, ...node.children];
2022-11-17 15:58:53 +08:00
// 如果存在返回图标名称
return iconName;
2022-10-15 13:32:12 +08:00
} else {
const svgNode = getSVGNode(iconDefaultPath);
2022-10-29 00:24:39 +08:00
node.children = [...svgNode, ...node.children];
2022-10-15 13:32:12 +08:00
}
}
2022-10-29 00:24:39 +08:00
}