|
| 1 | +/* eslint-disable */ |
| 2 | +/** |
| 3 | + * Sidebar Badge 插件 |
| 4 | + * |
| 5 | + * 功能:从 markdown 文件的 frontmatter 中读取 badge,自动添加到 sidebar |
| 6 | + * |
| 7 | + * @example |
| 8 | + * ```ts |
| 9 | + * // config.mts |
| 10 | + * import { SidebarBadgePlugin } from './plugins/badge' |
| 11 | + * |
| 12 | + * export default defineConfig({ |
| 13 | + * vite: { |
| 14 | + * plugins: [SidebarBadgePlugin({ debug: true })] |
| 15 | + * } |
| 16 | + * }) |
| 17 | + * ``` |
| 18 | + */ |
| 19 | + |
| 20 | +import fs from 'fs' |
| 21 | +import path from 'path' |
| 22 | +import { withBadge } from './utils' |
| 23 | + |
| 24 | +export interface SidebarBadgeOptions { |
| 25 | + /** |
| 26 | + * 源文件目录,默认为 'src' |
| 27 | + */ |
| 28 | + srcDir?: string |
| 29 | + /** |
| 30 | + * 是否启用调试日志 |
| 31 | + */ |
| 32 | + debug?: boolean |
| 33 | +} |
| 34 | + |
| 35 | +export function SidebarBadgePlugin(options?: SidebarBadgeOptions): any { |
| 36 | + let vpConfig: any = null |
| 37 | + const { srcDir = 'src', debug = false } = options || {} |
| 38 | + let badgeCount = 0 |
| 39 | + |
| 40 | + return { |
| 41 | + name: 'vitepress-sidebar-badge', |
| 42 | + |
| 43 | + async configResolved(config: any) { |
| 44 | + if (vpConfig) return |
| 45 | + |
| 46 | + vpConfig = config.vitepress |
| 47 | + if (!vpConfig) { |
| 48 | + console.warn('[SidebarBadge] VitePress config not found') |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + const { default: matter } = await import('gray-matter') |
| 54 | + |
| 55 | + const sidebar = vpConfig.site.themeConfig.sidebar |
| 56 | + if (!sidebar) { |
| 57 | + console.warn('[SidebarBadge] No sidebar config found') |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + badgeCount = processSidebar(sidebar, vpConfig.root || process.cwd(), srcDir, matter, debug) |
| 62 | + |
| 63 | + if (badgeCount > 0) { |
| 64 | + console.log(`✅ SidebarBadge: ${badgeCount} badge(s) applied`) |
| 65 | + } else if (debug) { |
| 66 | + console.log('ℹ️ SidebarBadge: No badges found') |
| 67 | + } |
| 68 | + } catch (error) { |
| 69 | + console.error('[SidebarBadge] Error:', error) |
| 70 | + } |
| 71 | + }, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * 处理 sidebar 配置 |
| 77 | + */ |
| 78 | +function processSidebar(sidebar: any, rootDir: string, srcDir: string, matter: any, debug: boolean): number { |
| 79 | + let count = 0 |
| 80 | + const processedItems = new Set<any>() |
| 81 | + |
| 82 | + if (Array.isArray(sidebar)) { |
| 83 | + count = processSidebarArray(sidebar, rootDir, srcDir, matter, debug, processedItems) |
| 84 | + } else if (typeof sidebar === 'object') { |
| 85 | + Object.keys(sidebar).forEach((key) => { |
| 86 | + const sidebarConfig = sidebar[key] |
| 87 | + if (Array.isArray(sidebarConfig)) { |
| 88 | + const dirName = key.replace(/^\//, '').replace(/\/$/, '') |
| 89 | + count += processSidebarArray(sidebarConfig, rootDir, srcDir, matter, debug, processedItems, dirName) |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + return count |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * 处理 sidebar 数组 |
| 99 | + */ |
| 100 | +function processSidebarArray( |
| 101 | + sidebarArray: any[], |
| 102 | + rootDir: string, |
| 103 | + srcDir: string, |
| 104 | + matter: any, |
| 105 | + debug: boolean, |
| 106 | + processedItems: Set<any>, |
| 107 | + dirName?: string, |
| 108 | +): number { |
| 109 | + let count = 0 |
| 110 | + |
| 111 | + sidebarArray.forEach((group) => { |
| 112 | + if (group.items && Array.isArray(group.items)) { |
| 113 | + const baseDir = group.base ? group.base.replace(/^\//, '').replace(/\/$/, '') : dirName |
| 114 | + |
| 115 | + group.items.forEach((item: any) => { |
| 116 | + if (item.link) { |
| 117 | + const badge = readBadgeFromFrontmatter(item.link, rootDir, srcDir, baseDir || '', matter, debug) |
| 118 | + if (badge && !processedItems.has(item)) { |
| 119 | + item.text = withBadge(item.text, badge) |
| 120 | + processedItems.add(item) |
| 121 | + count++ |
| 122 | + if (debug) { |
| 123 | + console.log(`[SidebarBadge] ✓ ${item.link}: ${badge}`) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + }) |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + return count |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * 从 markdown frontmatter 读取 badge |
| 136 | + */ |
| 137 | +function readBadgeFromFrontmatter( |
| 138 | + link: string, |
| 139 | + rootDir: string, |
| 140 | + srcDir: string, |
| 141 | + baseDir: string, |
| 142 | + matter: any, |
| 143 | + debug: boolean, |
| 144 | +): string | undefined { |
| 145 | + try { |
| 146 | + const filePath = path.join(rootDir, srcDir, baseDir, `${link}.md`) |
| 147 | + |
| 148 | + if (!fs.existsSync(filePath)) { |
| 149 | + return undefined |
| 150 | + } |
| 151 | + |
| 152 | + const content = fs.readFileSync(filePath, 'utf-8') |
| 153 | + const { data } = matter(content) |
| 154 | + |
| 155 | + return data.badge as string | undefined |
| 156 | + } catch (error) { |
| 157 | + if (debug) { |
| 158 | + console.warn(`[SidebarBadge] ✗ ${link}: ${error}`) |
| 159 | + } |
| 160 | + return undefined |
| 161 | + } |
| 162 | +} |
0 commit comments