website: title auto add icon.

This commit is contained in:
jaywcjlove
2022-10-15 13:32:12 +08:00
parent a24f6a91a4
commit cadc7dad14
5 changed files with 28 additions and 2 deletions

View File

@ -2,7 +2,7 @@ import fs from 'fs-extra';
import path from 'path';
import { getSVGNode } from './getSVGNode.mjs';
const ICONS_PATH = path.resolve(process.cwd(), 'scripts/assets')
export const ICONS_PATH = path.resolve(process.cwd(), 'scripts/assets')
export function homeCardIcons(node, parent, isHome) {
if (isHome && node && node.type === 'element' && node.properties?.class?.includes('home-card')) {

View File

@ -0,0 +1,19 @@
import fs from 'fs-extra';
import path from 'path';
import { getSVGNode } from './getSVGNode.mjs';
import { ICONS_PATH } from './homeCardIcons.mjs';
export function rehypeTitle(node, iconName) {
if (node.type === 'element' && node.tagName === 'h1' && iconName !== 'index') {
const iconPath = path.resolve(ICONS_PATH, `${iconName}.svg`);
const iconDefaultPath = path.resolve(ICONS_PATH, `list.svg`);
const iconExist = fs.existsSync(iconPath);
if (iconExist) {
const svgNode = getSVGNode(iconPath);
node.children = [ ...svgNode, ...node.children ];
} else {
const svgNode = getSVGNode(iconDefaultPath);
node.children = [ ...svgNode, ...node.children ];
}
}
}