From 9a80036d6647aa7de3b98878443bf302dccfd5b7 Mon Sep 17 00:00:00 2001 From: Ri-Nai Date: Thu, 25 Sep 2025 12:16:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=20VitePress=20?= =?UTF-8?q?=E7=9A=84=E7=B1=BB=E5=9E=8B=E5=AE=9A=E4=B9=89=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=AF=BC=E8=88=AA=E5=92=8C=E4=BE=A7=E8=BE=B9=E6=A0=8F=E7=94=9F?= =?UTF-8?q?=E6=88=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vitepress/navSidebar.ts | 48 +++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/.vitepress/navSidebar.ts b/.vitepress/navSidebar.ts index 613a810..8b2a2a4 100644 --- a/.vitepress/navSidebar.ts +++ b/.vitepress/navSidebar.ts @@ -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 +export type SidebarItem = DefaultTheme.SidebarItem +export type NavItem = DefaultTheme.NavItem +export type Sidebar = DefaultTheme.Sidebar const DOC_EXT = ['.md'] const EXCLUDED_DIRS = new Set([ @@ -42,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 @@ -52,28 +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))) - const readmeURI = readme ? `/${encodeURI(dir)}/${encodeURI(readme)}` : "/"; 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, - link: readmeURI, - items: items.filter((i) => i.link !== readmeURI), + link: sectionLink, + items: sectionItems, }, ] - if (readme) { - nav.push({ text: dir, link: readmeURI }) - } else { - nav.push({ text: dir, link: items[0].link! }) - } + + nav.push({ + text: dir, + link: sectionLink, + }) } else { nav.push({ text: dir, link: `/${encodeURI(dir)}/` }) }