reference/scripts/utils/homeCardIcons.mjs

47 lines
1.4 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) {
2022-11-04 00:33:59 +08:00
if (
isHome &&
node &&
node.type === 'element' &&
(node.properties?.class || node.properties?.className)?.includes('contributing')
) {
2022-11-03 11:49:21 +08:00
const info = node.properties['data-info'];
if (!info) {
node.properties['data-info'] = '👆待完善需要您的参与';
}
}
2022-11-04 00:33:59 +08:00
if (
isHome &&
node &&
node.type === 'element' &&
(node.properties?.class || node.properties?.className)?.includes('home-card')
) {
2022-09-30 14:15:26 +08:00
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
}