reference/scripts/utils/rehypeTitle.mjs

18 lines
671 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);
node.children = [ ...svgNode, ...node.children ];
} else {
const svgNode = getSVGNode(iconDefaultPath);
node.children = [ ...svgNode, ...node.children ];
}
}
}