fix: fix search error & add name field in data.json #105

This commit is contained in:
jaywcjlove
2022-11-21 13:54:18 +08:00
parent 05f3df7e04
commit b473d99111
5 changed files with 178 additions and 130 deletions

View File

@ -1,7 +1,11 @@
import fs from 'fs-extra';
import path from 'path';
import { getCodeString } from 'rehype-rewrite';
import { getSVGNode, ICONS_PATH } from './getSVGNode.mjs';
const resultHomeCard = {};
const COLOR_REG = /background:(\s+)?rgb\(([\d]+\s+[\d]+\s+[\d]+(\s+)?)\);?/gi;
export function homeCardIcons(node, parent, isHome) {
if (
isHome &&
@ -22,16 +26,23 @@ export function homeCardIcons(node, parent, isHome) {
) {
node.children = node.children.map((child) => {
const href = child.properties?.href;
if (href) {
if (href && href.endsWith('.md')) {
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'];
const labelNode = {
type: 'element',
tagName: 'span',
children: child.children,
};
const title = getCodeString(child.children);
if (iconExist) {
const svgNode = getSVGNode(iconPath);
child.children = [...svgNode, labelNode];
@ -39,8 +50,15 @@ export function homeCardIcons(node, parent, isHome) {
const svgNode = getSVGNode(iconDefaultPath);
child.children = [...svgNode, labelNode];
}
resultHomeCard[iconName] = {
md: iconName,
title: title,
rgb: color,
tags: tags ? tags.split('/') : [],
};
}
return child;
});
}
return resultHomeCard;
}