reference/scripts/utils/homeCardIcons.mjs

65 lines
2.1 KiB
JavaScript
Raw Normal View History

2022-09-30 14:15:26 +08:00
import fs from 'fs-extra';
import path from 'path';
import { getCodeString } from 'rehype-rewrite';
import { getSVGNode, ICONS_PATH } from './getSVGNode.mjs';
2022-09-30 14:15:26 +08:00
const resultHomeCard = {};
const COLOR_REG = /background:(\s+)?rgb\(([\d]+\s+[\d]+\s+[\d]+(\s+)?)\);?/gi;
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 && href.endsWith('.md')) {
2022-09-30 14:15:26 +08:00
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);
let color = '';
child.properties.style = child.properties.style.replace(COLOR_REG, (str) => {
color = str.replace(COLOR_REG, '$2');
return str.replace(/(\);)/, '/ var(--bg-opacity)$1');
});
const tags = child.properties['data-lang'];
2022-09-30 14:15:26 +08:00
const labelNode = {
type: 'element',
tagName: 'span',
children: child.children,
2022-10-29 00:24:39 +08:00
};
const title = getCodeString(child.children);
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
}
resultHomeCard[iconName] = {
md: iconName,
title: title,
rgb: color,
tags: tags ? tags.split('/') : [],
};
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
}
return resultHomeCard;
2022-10-29 00:24:39 +08:00
}