Skip to content

Commit 6c6d5c9

Browse files
docs: ✏️ 文档侧边栏新增版本号显示
1 parent 372735a commit 6c6d5c9

39 files changed

Lines changed: 473 additions & 36 deletions

‎docs/.vitepress/config.mts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @Author: weisheng
33
* @Date: 2023-07-27 10:26:09
4-
* @LastEditTime: 2025-07-07 20:46:21
4+
* @LastEditTime: 2025-07-08 17:45:58
55
* @LastEditors: weisheng
66
* @Description:
77
* @FilePath: /wot-design-uni/docs/.vitepress/config.mts
@@ -11,6 +11,7 @@ import { defineConfig } from 'vitepress';
1111
import viteCompression from 'vite-plugin-compression'
1212
import { fileURLToPath, URL } from 'node:url'
1313
import { MarkdownTransform } from './plugins/markdown-transform'
14+
import { VersionBadgePlugin } from './plugins/version-badge'
1415
import llmstxt from 'vitepress-plugin-llms'
1516
import enUS from './locales/en-US'
1617
import zhCN from './locales/zh-CN'
@@ -22,6 +23,7 @@ export default defineConfig({
2223
domain: 'https://wot-design-uni.cn',
2324
}),
2425
MarkdownTransform(),
26+
VersionBadgePlugin(),
2527
viteCompression({
2628
verbose: true,
2729
disable: false,
@@ -62,6 +64,12 @@ export default defineConfig({
6264
replacement: fileURLToPath(
6365
new URL('./theme/components/VPNavBar.vue', import.meta.url)
6466
)
67+
},
68+
{
69+
find: /^.*\/VPSidebarItem\.vue$/,
70+
replacement: fileURLToPath(
71+
new URL('./theme/components/VPSidebarItem.vue', import.meta.url)
72+
)
6573
}
6674
]
6775
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// 自动生成的版本数据
2+
// 此文件由插件自动生成,请勿手动修改
3+
// 如需修改版本信息,请在对应的 Markdown 文件 frontmatter 中修改
4+
5+
export const versionData = {
6+
"/component/count-to": "1.3.8",
7+
"/component/keyboard": "1.3.10",
8+
"/component/root-portal": "$LOWEST_VERSION$",
9+
"/component/signature": "1.6.0"
10+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import type { Plugin } from 'vite'
2+
import { readFileSync, existsSync, writeFileSync } from 'fs'
3+
import { resolve } from 'path'
4+
import fs from 'fs'
5+
6+
// 从 Markdown 文件中提取版本信息
7+
function extractVersionFromMarkdown(filePath: string): string | null {
8+
if (!existsSync(filePath)) return null
9+
10+
try {
11+
const content = readFileSync(filePath, 'utf-8')
12+
const frontmatterMatch = content.match(/^---\s*\n([\s\S]*?)\n---\s*\n/)
13+
14+
if (frontmatterMatch) {
15+
const frontmatter = frontmatterMatch[1]
16+
const versionMatch = frontmatter.match(/version:\s*(['"]?)([^'"\n]+)\1/)
17+
18+
if (versionMatch) {
19+
return versionMatch[2].trim()
20+
}
21+
}
22+
} catch (e) {
23+
console.warn(`Failed to read file ${filePath}:`, e)
24+
}
25+
26+
return null
27+
}
28+
29+
// 扫描组件文档目录,提取版本信息
30+
function scanComponentVersions(): Record<string, string> {
31+
const versionData: Record<string, string> = {}
32+
const componentDir = resolve(__dirname, '../../component')
33+
34+
if (!existsSync(componentDir)) {
35+
console.warn('Component directory not found:', componentDir)
36+
return versionData
37+
}
38+
39+
try {
40+
const files = fs.readdirSync(componentDir).filter((file: string) => file.endsWith('.md'))
41+
42+
files.forEach((filename: string) => {
43+
const filePath = resolve(componentDir, filename)
44+
const version = extractVersionFromMarkdown(filePath)
45+
46+
if (version) {
47+
const path = `/component/${filename.replace('.md', '')}`
48+
versionData[path] = version
49+
console.log(`✅ Found version info in ${filename}:`, version)
50+
}
51+
})
52+
} catch (e) {
53+
console.warn('Failed to scan component directory:', e)
54+
}
55+
56+
return versionData
57+
}
58+
59+
// 生成版本数据文件
60+
function generateVersionData(): string {
61+
const versionData = scanComponentVersions()
62+
63+
return `// 自动生成的版本数据
64+
// 此文件由插件自动生成,请勿手动修改
65+
// 如需修改版本信息,请在对应的 Markdown 文件 frontmatter 中修改
66+
67+
export const versionData = ${JSON.stringify(versionData, null, 2)}
68+
`
69+
}
70+
71+
export function VersionBadgePlugin(): Plugin {
72+
return {
73+
name: 'version-badge',
74+
configResolved() {
75+
// 在配置解析后生成版本数据
76+
try {
77+
const versionDataContent = generateVersionData()
78+
const outputPath = resolve(__dirname, '../config/version-data.ts')
79+
80+
writeFileSync(outputPath, versionDataContent, 'utf-8')
81+
console.log('✅ Version data generated successfully')
82+
} catch (e) {
83+
console.error('❌ Failed to generate version data:', e)
84+
}
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)