import type { DefaultTheme } from 'vitepress/theme' interface Page { title: string; path: string; child?: Page[]; collapsed?: boolean, // 添加折叠,false:显示,true:隐藏 } const menu : Page[] = [ { title: '硬件产品', path: '/Hardware', child: [ { title: 'Gate', path: '/Gate', collapsed: false, child: [ { title: 'aGate 1.0/1.1', path: '/aGate_1.0.md' }, { title: 'aGate 1.2', path: '/aGate_1.2.md' }, { title: 'aGate 1.3', path: '/aGate_1.3.md' }, { title: 'aGate 1.3.1', path: '/aGate_1.3.1.md' }, { title: 'MSA-C', path: '/MSA_C.md' }, { title: 'MSA-C2', path: '/MSA_C2.md' } ] }, { title: 'Power', path: '/Power', collapsed: false, child: [ { title: 'aPower 1.0', path: '/aPower_1.0.md' }, { title: 'aPower 1.2', path: '/aPower_1.2.md' }, { title: 'aPower 1.3', path: '/aPower_1.3.md' }, { title: 'aPower 2', path: '/aPower_2.md' }, { title: 'aPower S', path: '/aPower_S.md' } ] }, { title: '配件', path: '/Other', collapsed: false, child: [ { title: 'aPbox 1.0', path: '/aPbox_1.0.md' }, { title: 'aPbox 2.0', path: '/aPbox_2.0.md' }, { title: 'Meter Kit', path: '/MeterKit.md' }, { title: 'Generator Module', path: '/GeneratorModule.md' } ] } ] }, { title: '软件功能', path: '/Software', child: [ { title: '模式管理', path: '/ModeManagement.md' }, { title: 'PCS', path: '/PCS.md' }, { title: '黑启动', path: '/BlackStart.md' }, { title: '光伏调频', path: '/PV_FreqWatt.md' }, { title: '电池加热', path: '/BatteryHeating.md' }, { title: 'SOC均衡', path: '/SocBalance.md' }, { title: '调度配置脚本化', path: '/ScriptingForSchedulingConfiguration.md' }, { title: '能量统计', path: '/EnergyStatistics.md' } ] }, { title: '系统', path: '/System', child: [ { title: '系统搭配', path: '/SystemConfig.md' }, { title: '系统配电', path: '/SystemDistribution.md' } ] }, { title: '其他', path: '/Other', child: [ { title: '资费套餐模版', path: '/TariffScheduleTemplates.md' } ] } ] type NavItem = DefaultTheme.NavItem type ChildNavItem = (DefaultTheme.NavItemComponent | DefaultTheme.NavItemChildren | DefaultTheme.NavItemWithLink) type SubChildNavItem = DefaultTheme.NavItemWithLink type SidebarItem = DefaultTheme.SidebarItem type SidebarMulti = DefaultTheme.SidebarMulti interface MenuConfig { nav?: NavItem[], sidebar?: SidebarMulti } // 遍历导航栏,items最大深度3 function traverseNavigation(pages: Page[]): NavItem[] | undefined { const navItem = pages.map(item => { if (item.child && item.child.length > 0) { const parentPath = item.path const childNavItem = item.child.map(childItem => { if (childItem.child && childItem.child.length > 0) { const childPath = parentPath + childItem.path const subChildItem = childItem.child.map(subChildItem => { return { text: subChildItem.title, link: childPath + subChildItem.path } }) return { text: childItem.title, items: subChildItem } } else { return { text: childItem.title, link: parentPath + childItem.path } } }) return { text: item.title, items: childNavItem } } else { return { text: item.title, link: item.path } } }) if (navItem.length === 0) { return undefined } else { return navItem } } function traverseSidebar(path: string, pages: Page[]): SidebarItem[] { let config : SidebarItem[] = [] for (const page of pages) { // 将当前页面加入配置中 if (page.child && page.child.length > 0) { const childConfig = traverseSidebar(path + page.path, page.child); // 递归遍历子页面 config.push({ text: page.title, link: path + page.path, collapsed: page.collapsed, items: childConfig }) } else { config.push({ text: page.title, link: path + page.path, collapsed: page.collapsed }) } } return config } function traverseMainSidebar(pages: Page[]): SidebarMulti { let config : SidebarMulti = {} for (const page of pages) { // 将当前页面加入配置中 if (page.child && page.child.length > 0) { const childConfig = traverseSidebar("", page.child); // 递归遍历子页面 config[page.path] = { base: page.path, items: [ { text: page.title, items: childConfig } ] } } else { config[page.path] = { base: page.path, items: [ { text: page.title } ] } } } return config } const config : MenuConfig = { nav: traverseNavigation(menu), // sidebar: [ // { // text: '目录', // items: traverseSidebar('', menu) // } // ] sidebar: traverseMainSidebar(menu) } export default config