Skip to content

Commit 4f76146

Browse files
committed
feat: handle multilevel sidebar base config
1 parent f6430ac commit 4f76146

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/@vuepress/theme-default/util/index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export function resolveMatchingConfig (regularPath, config) {
200200
config: config
201201
}
202202
}
203+
203204
for (const base in config) {
204205
if (ensureEndingSlash(regularPath).indexOf(encodeURI(base)) === 0) {
205206
return {
@@ -217,6 +218,29 @@ function ensureEndingSlash (path) {
217218
: path + '/'
218219
}
219220

221+
const baseMap = new Map()
222+
/**
223+
* handle multilevel sidebar base config
224+
* @param {*} item
225+
* @param {*} base
226+
* @param {*} groupDepth
227+
* @returns
228+
*/
229+
function resolveBase (item, base, groupDepth) {
230+
if (groupDepth <= 2) {
231+
!baseMap.has(groupDepth.toString()) && baseMap.set(groupDepth.toString(), base)
232+
return base
233+
}
234+
235+
if (!baseMap.has(groupDepth.toString())) {
236+
const prev = baseMap.get((groupDepth - 1).toString())
237+
baseMap.set(groupDepth.toString(), `${prev + item.title}\/`)
238+
return baseMap.get(groupDepth.toString())
239+
} else {
240+
return baseMap.get(groupDepth.toString())
241+
}
242+
}
243+
220244
function resolveItem (item, pages, base, groupDepth = 1) {
221245
if (typeof item === 'string') {
222246
return resolvePage(pages, item, base)
@@ -237,7 +261,7 @@ function resolveItem (item, pages, base, groupDepth = 1) {
237261
title: item.title,
238262
sidebarDepth: item.sidebarDepth,
239263
initialOpenGroupIndex: item.initialOpenGroupIndex,
240-
children: children.map(child => resolveItem(child, pages, base, groupDepth + 1)),
264+
children: children.map(child => resolveItem(child, pages, resolveBase(item, base, groupDepth + 1), groupDepth + 1)),
241265
collapsable: item.collapsable !== false
242266
}
243267
}

packages/@vuepress/types/src/theme-default.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export type SidebarItem4Group = {
8484
/**
8585
* Sidebar children.
8686
*/
87-
children?: Array<SidebarItem4Shortcut>;
87+
children?: Array<SidebarItem4Shortcut | SidebarItem4Group>;
8888
};
8989

9090
/**

0 commit comments

Comments
 (0)