1+ import type {
2+ DocsSidebarConfig ,
3+ DocsSidebarGroup ,
4+ DocsSidebarItem ,
5+ } from '@142vip/vuepress'
16import type { AutoLinkOptions , SidebarItemOptions } from 'vuepress-theme-hope'
27import { VipNodeJS } from '@142vip/utils'
38
4- /** 侧边栏直链项 */
5- export interface DocsSidebarLink {
6- text : string
7- link : string
8- }
9-
10- /** 侧边栏分组项 */
11- export interface DocsSidebarGroup {
12- text : string
13- prefix ?: string
14- link ?: string
15- collapsible ?: boolean
16- expanded ?: boolean
17- children : DocsSidebarItem [ ]
18- }
19-
20- export type DocsSidebarItem = DocsSidebarLink | DocsSidebarGroup
21-
22- export type DocsSidebarConfig = DocsSidebarItem [ ]
23-
24- function isSidebarGroup ( item : DocsSidebarItem ) : item is DocsSidebarGroup {
25- return Array . isArray ( ( item as DocsSidebarGroup ) . children )
26- }
27-
289function readPermalink ( mdFilePath : string ) : string | null {
2910 if ( ! VipNodeJS . existPath ( mdFilePath ) ) {
3011 return null
@@ -59,6 +40,8 @@ function resolveMdLink(
5940 * 本仓库大量页面 permalink 与文件名不一致(如 `html.md` → `/front-end/base-begin/html-correlation.html`),
6041 * 故在配置阶段按文件解析并替换 `link`;无法单靠 `vuepress.config.ts` 原生选项完成。
6142 *
43+ * 侧栏结构类型见 `@142vip/vuepress` 的 `DocsSidebar*`;本函数为站点侧解析工具。
44+ *
6245 * @param sidebar 模块侧边栏原始配置(可含 `.md` 链接)
6346 * @param docsBasePath `docs/` 下的相对目录,如 `server-end/framework/egg`
6447 */
@@ -68,14 +51,15 @@ export function resolveSidebarPermalinks(
6851) : SidebarItemOptions [ ] {
6952 const walk = ( items : DocsSidebarItem [ ] , groupPrefix = '' ) : SidebarItemOptions [ ] =>
7053 items . map ( ( item ) => {
71- if ( isSidebarGroup ( item ) ) {
54+ if ( Array . isArray ( ( item as DocsSidebarGroup ) . children ) ) {
55+ const group = item as DocsSidebarGroup
7256 return {
73- ...item ,
74- children : walk ( item . children , item . prefix ?? '' ) ,
57+ ...group ,
58+ children : walk ( group . children , group . prefix ?? '' ) ,
7559 }
7660 }
7761
78- if ( ! item . link . endsWith ( '.md' ) ) {
62+ if ( ! item . link ? .endsWith ( '.md' ) ) {
7963 return item as AutoLinkOptions
8064 }
8165
0 commit comments