This commit is contained in:
2026-02-21 18:48:18 +08:00
parent 9f3fd2ae9d
commit 8a55430d55
20 changed files with 2025 additions and 97 deletions

View File

@@ -12,14 +12,11 @@ interface SidebarItem {
}
interface SidebarGroup {
text: string
title: string
path: string
items: SidebarItem[]
}
interface SidebarConfig {
[path: string]: SidebarGroup[]
}
function getTitleFromFile(filePath: string): string {
const content = fs.readFileSync(filePath, 'utf-8')
@@ -57,8 +54,8 @@ function getSidebarOrder(filePath: string, fileName: string): number {
return 999
}
function generateSidebar(): SidebarConfig {
const sidebar: SidebarConfig = {}
function generateSidebar(): SidebarGroup[] {
const sidebar: SidebarGroup[] = []
const entries = fs.readdirSync(docsDir, { withFileTypes: true })
@@ -94,78 +91,30 @@ function generateSidebar(): SidebarConfig {
}
})
const dirName = entry.name.charAt(0).toUpperCase() + entry.name.slice(1)
sidebar[`/${entry.name}/`] = [
{
text: `${dirName} 笔记`,
items
}
]
sidebar.push({
title: entry.name,
path: `/${entry.name}/`,
items
})
}
return sidebar
}
function generateNav(sidebar: SidebarConfig) {
return Object.keys(sidebar).map(p => {
const name = p.replace(/\//g, '').charAt(0).toUpperCase() + p.replace(/\//g, '').slice(1)
return {
text: name,
link: p
}
})
}
function formatObject(obj: unknown, indent: number = 0): string {
const spaces = ' '.repeat(indent)
const innerSpaces = ' '.repeat(indent + 2)
if (Array.isArray(obj)) {
if (obj.length === 0) return '[]'
const items = obj.map(item => formatObject(item, indent + 2))
return `[\n${items.map(i => innerSpaces + i).join(',\n')}\n${spaces}]`
}
if (typeof obj === 'object' && obj !== null) {
const entries = Object.entries(obj)
if (entries.length === 0) return '{}'
const items = entries.map(([key, value]) => {
const formattedValue = formatObject(value, indent + 2)
return `${innerSpaces}'${key}': ${formattedValue}`
})
return `{\n${items.join(',\n')}\n${spaces}}`
}
if (typeof obj === 'string') {
return `'${obj}'`
}
return String(obj)
}
function updateConfig() {
const sidebar = generateSidebar()
const nav = generateNav(sidebar)
const configPath = path.resolve(__dirname, '../docs/.vitepress/config.mts')
let config = fs.readFileSync(configPath, 'utf-8')
const outputPath = path.resolve(__dirname, '../docs/.vitepress/theme/data/sidebar.json')
const outputDir = path.dirname(outputPath)
const sidebarStr = formatObject(sidebar, 4)
const navStr = formatObject(nav, 4)
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true })
}
const sidebarRegex = /sidebar:\s*\{\s*\n\s*\}/
const navRegex = /nav:\s*\[\s*\n\s*\]/
fs.writeFileSync(outputPath, JSON.stringify(sidebar, null, 2))
config = config.replace(sidebarRegex, `sidebar: ${sidebarStr}`)
config = config.replace(navRegex, `nav: ${navStr}`)
fs.writeFileSync(configPath, config)
console.log('Sidebar config updated:')
console.log('Sidebar JSON generated:')
console.log(JSON.stringify(sidebar, null, 2))
console.log('\nNav config updated:')
console.log(JSON.stringify(nav, null, 2))
}
updateConfig()