Files
.github
.husky
docs
scripts
assets
nodes
utils
anchorPoint.mjs
childs.mjs
darkMode.mjs
getSVGNode.mjs
getTocsTree.mjs
homeCardIcons.mjs
panelAddNumber.mjs
rehypePreviewHTML.mjs
rehypeTitle.mjs
rehypeUrls.mjs
tooltips.mjs
build.mjs
create.mjs
index.mjs
style.css
watch.mjs
.gitignore
.lintstagedrc
.prettierignore
.prettierrc
CONTRIBUTING.md
Dockerfile
LICENSE
README.md
package.json
renovate.json
reference/scripts/utils/homeCardIcons.mjs
2022-10-29 00:24:39 +08:00

31 lines
1.0 KiB
JavaScript

import fs from 'fs-extra';
import path from 'path';
import { getSVGNode, ICONS_PATH } from './getSVGNode.mjs';
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,
};
if (iconExist) {
const svgNode = getSVGNode(iconPath);
child.children = [...svgNode, labelNode];
} else {
const svgNode = getSVGNode(iconDefaultPath);
child.children = [...svgNode, labelNode];
}
}
return child;
});
}
}