diff --git a/packages/@vuepress/theme-default/util/index.js b/packages/@vuepress/theme-default/util/index.js index 92fcd3b311..6ec5b41b2d 100644 --- a/packages/@vuepress/theme-default/util/index.js +++ b/packages/@vuepress/theme-default/util/index.js @@ -200,6 +200,7 @@ export function resolveMatchingConfig (regularPath, config) { config: config } } + for (const base in config) { if (ensureEndingSlash(regularPath).indexOf(encodeURI(base)) === 0) { return { @@ -217,6 +218,29 @@ function ensureEndingSlash (path) { : path + '/' } +const baseMap = new Map() +/** + * handle multilevel sidebar base config + * @param {*} item + * @param {*} base + * @param {*} groupDepth + * @returns + */ +function resolveBase (item, base, groupDepth) { + if (groupDepth <= 2) { + !baseMap.has(groupDepth.toString()) && baseMap.set(groupDepth.toString(), base) + return base + } + + if (!baseMap.has(groupDepth.toString())) { + const prev = baseMap.get((groupDepth - 1).toString()) + baseMap.set(groupDepth.toString(), `${prev + item.title}\/`) + return baseMap.get(groupDepth.toString()) + } else { + return baseMap.get(groupDepth.toString()) + } +} + function resolveItem (item, pages, base, groupDepth = 1) { if (typeof item === 'string') { return resolvePage(pages, item, base) @@ -237,7 +261,7 @@ function resolveItem (item, pages, base, groupDepth = 1) { title: item.title, sidebarDepth: item.sidebarDepth, initialOpenGroupIndex: item.initialOpenGroupIndex, - children: children.map(child => resolveItem(child, pages, base, groupDepth + 1)), + children: children.map(child => resolveItem(child, pages, resolveBase(item, base, groupDepth + 1), groupDepth + 1)), collapsable: item.collapsable !== false } } diff --git a/packages/@vuepress/types/src/theme-default.ts b/packages/@vuepress/types/src/theme-default.ts index c086041e2a..bc55cd7446 100644 --- a/packages/@vuepress/types/src/theme-default.ts +++ b/packages/@vuepress/types/src/theme-default.ts @@ -84,7 +84,7 @@ export type SidebarItem4Group = { /** * Sidebar children. */ - children?: Array; + children?: Array; }; /**