mirror of
https://github.com/Gar-b-age/CookLikeHOC.git
synced 2025-12-26 03:48:31 +08:00
Merge pull request #155 from Ri-Nai/feat/optimize-sidebar
feat(vitepress): 重构导航与侧边栏生成脚本并修复错误
This commit is contained in:
commit
8f44b33488
@ -1,8 +1,10 @@
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import type { DefaultTheme } from 'vitepress'
|
||||
|
||||
export type SidebarItem = { text: string; link?: string; items?: SidebarItem[] }
|
||||
export type Sidebar = Record<string, SidebarItem[]>
|
||||
export type SidebarItem = DefaultTheme.SidebarItem
|
||||
export type NavItem = DefaultTheme.NavItem
|
||||
export type Sidebar = DefaultTheme.Sidebar
|
||||
|
||||
const DOC_EXT = ['.md']
|
||||
const EXCLUDED_DIRS = new Set([
|
||||
@ -10,6 +12,8 @@ const EXCLUDED_DIRS = new Set([
|
||||
'.github',
|
||||
'.vitepress',
|
||||
'node_modules',
|
||||
'images',
|
||||
'docker_support',
|
||||
'public',
|
||||
'docs',
|
||||
'images',
|
||||
@ -40,9 +44,13 @@ export function generateNavAndSidebar(rootDir: string) {
|
||||
.filter((e) => !EXCLUDED_DIRS.has(e) && !e.startsWith('.'))
|
||||
sections.sort(sortByPinyinOrName)
|
||||
|
||||
const nav: { text: string; link: string }[] = []
|
||||
const nav: NavItem[] = []
|
||||
const sidebar: Sidebar = {}
|
||||
|
||||
// This is the item type we generate from files. It has a required text and link.
|
||||
// It is compatible with both NavItem and SidebarItem.
|
||||
type LinkItem = { text: string; link: string }
|
||||
|
||||
for (const dir of sections) {
|
||||
const abs = path.join(rootDir, dir)
|
||||
const files = fs
|
||||
@ -50,27 +58,40 @@ export function generateNavAndSidebar(rootDir: string) {
|
||||
.filter((f) => isMarkdown(path.join(abs, f)))
|
||||
.sort(sortByPinyinOrName)
|
||||
|
||||
// Build sidebar for this section
|
||||
const items: SidebarItem[] = files.map((f) => ({
|
||||
const items: LinkItem[] = files.map((f) => ({
|
||||
text: titleFromName(f),
|
||||
link: `/${encodeURI(dir)}/${encodeURI(f)}`,
|
||||
}))
|
||||
|
||||
// Find README.md、readme.md、index.md
|
||||
const readme = ['README.md', 'readme.md', 'index.md'].find((n) => fs.existsSync(path.join(abs, n)))
|
||||
|
||||
if (items.length > 0) {
|
||||
const readme = ['README.md', 'readme.md', 'index.md'].find((n) =>
|
||||
fs.existsSync(path.join(abs, n)),
|
||||
)
|
||||
const readmeURI = readme ? `/${encodeURI(dir)}/${encodeURI(readme)}` : undefined
|
||||
|
||||
let sectionLink: string
|
||||
let sectionItems: LinkItem[]
|
||||
|
||||
if (readmeURI) {
|
||||
sectionLink = readmeURI
|
||||
sectionItems = items.filter((i) => i.link !== readmeURI)
|
||||
} else {
|
||||
sectionLink = items[0].link!
|
||||
sectionItems = items.slice(1)
|
||||
}
|
||||
|
||||
sidebar[`/${dir}/`] = [
|
||||
{
|
||||
text: dir,
|
||||
items,
|
||||
link: sectionLink,
|
||||
items: sectionItems,
|
||||
},
|
||||
]
|
||||
if (readme) {
|
||||
nav.push({ text: dir, link: `/${encodeURI(dir)}/${encodeURI(readme)}` })
|
||||
} else {
|
||||
nav.push({ text: dir, link: items[0].link! })
|
||||
}
|
||||
|
||||
nav.push({
|
||||
text: dir,
|
||||
link: sectionLink,
|
||||
})
|
||||
} else {
|
||||
nav.push({ text: dir, link: `/${encodeURI(dir)}/` })
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user