reference/scripts/utils/homeCardIcons.mjs

31 lines
1.0 KiB
JavaScript
Raw Normal View History

2022-09-30 14:15:26 +08:00
import fs from 'fs-extra';
import path from 'path';
import { getSVGNode, ICONS_PATH } from './getSVGNode.mjs';
2022-09-30 14:15:26 +08:00
export function homeCardIcons(node, parent, isHome) {
if (isHome && node && node.type === 'element' && node.properties?.class?.includes('home-card')) {
node.children = node.children.map((child) => {
const href = child.properties?.href;
if (href) {
const iconName = path.basename(href, '.md');
const iconPath = path.resolve(ICONS_PATH, `${iconName}.svg`);
const iconDefaultPath = path.resolve(ICONS_PATH, `list.svg`);
const iconExist = fs.existsSync(iconPath);
const labelNode = {
type: 'element',
tagName: 'span',
children: child.children,
2022-10-29 00:24:39 +08:00
};
2022-09-30 14:15:26 +08:00
if (iconExist) {
const svgNode = getSVGNode(iconPath);
2022-10-29 00:24:39 +08:00
child.children = [...svgNode, labelNode];
2022-09-30 14:15:26 +08:00
} else {
const svgNode = getSVGNode(iconDefaultPath);
2022-10-29 00:24:39 +08:00
child.children = [...svgNode, labelNode];
2022-09-30 14:15:26 +08:00
}
}
2022-10-29 00:24:39 +08:00
return child;
});
2022-09-30 14:15:26 +08:00
}
2022-10-29 00:24:39 +08:00
}