diff --git a/packages/@vuepress/theme-default/components/Page.vue b/packages/@vuepress/theme-default/components/Page.vue index d086c0b8ba..d1d0649ebc 100644 --- a/packages/@vuepress/theme-default/components/Page.vue +++ b/packages/@vuepress/theme-default/components/Page.vue @@ -177,13 +177,7 @@ function resolveNext (page, items) { function find (page, items, offset) { const res = [] - items.forEach(item => { - if (item.type === 'group') { - res.push(...item.children || []) - } else { - res.push(item) - } - }) + flattern(items, res) for (let i = 0; i < res.length; i++) { const cur = res[i] if (cur.type === 'page' && cur.path === decodeURIComponent(page.path)) { @@ -191,6 +185,17 @@ function find (page, items, offset) { } } } + +function flattern (items, res) { + for (let i = 0, l = items.length; i < l; i++) { + if (items[i].type === 'group') { + flattern(items[i].children || [], res) + } else { + res.push(items[i]) + } + } +} + diff --git a/packages/@vuepress/theme-default/components/SidebarGroup.vue b/packages/@vuepress/theme-default/components/SidebarGroup.vue index 4b5b756536..3cc11c4b12 100644 --- a/packages/@vuepress/theme-default/components/SidebarGroup.vue +++ b/packages/@vuepress/theme-default/components/SidebarGroup.vue @@ -1,9 +1,34 @@ diff --git a/packages/@vuepress/theme-default/components/SidebarLink.vue b/packages/@vuepress/theme-default/components/SidebarLink.vue index 4b08380ed3..6a45295cac 100644 --- a/packages/@vuepress/theme-default/components/SidebarLink.vue +++ b/packages/@vuepress/theme-default/components/SidebarLink.vue @@ -4,9 +4,22 @@ import { isActive, hashRE, groupHeaders } from '../util' export default { functional: true, - props: ['item'], + props: ['item', 'sidebarDepth'], - render (h, { parent: { $page, $site, $route }, props: { item }}) { + render (h, + { + parent: { + $page, + $site, + $route, + $themeConfig, + $themeLocaleConfig + }, + props: { + item, + sidebarDepth + } + }) { // use custom active class matching logic // due to edge case of paths ending with / + hash const selfActive = isActive($route, item.path) @@ -16,11 +29,17 @@ export default { ? selfActive || item.children.some(c => isActive($route, item.basePath + '#' + c.slug)) : selfActive const link = renderLink(h, item.path, item.title || item.path, active) - const configDepth = $page.frontmatter.sidebarDepth != null - ? $page.frontmatter.sidebarDepth - : $site.themeConfig.sidebarDepth + + const configDepth = $page.frontmatter.sidebarDepth || + sidebarDepth || + $themeLocaleConfig.sidebarDepth || + $themeConfig.sidebarDepth + const maxDepth = configDepth == null ? 1 : configDepth - const displayAllHeaders = !!$site.themeConfig.displayAllHeaders + + const displayAllHeaders = $themeLocaleConfig.displayAllHeaders || + $themeConfig.displayAllHeaders + if (item.type === 'auto') { return [link, renderChildren(h, item.children, item.basePath, $route, maxDepth)] } else if ((active || displayAllHeaders) && item.headers && !hashRE.test(item.path)) { @@ -64,6 +83,7 @@ function renderChildren (h, children, path, route, maxDepth, depth = 1) { font-size 0.95em a.sidebar-link + font-size 1em font-weight 400 display inline-block color $textColor diff --git a/packages/@vuepress/theme-default/components/SidebarLinks.vue b/packages/@vuepress/theme-default/components/SidebarLinks.vue new file mode 100644 index 0000000000..5cb9d474c4 --- /dev/null +++ b/packages/@vuepress/theme-default/components/SidebarLinks.vue @@ -0,0 +1,86 @@ + + + diff --git a/packages/@vuepress/theme-default/styles/theme.styl b/packages/@vuepress/theme-default/styles/theme.styl index 1f552a3ed3..d2451ed58a 100644 --- a/packages/@vuepress/theme-default/styles/theme.styl +++ b/packages/@vuepress/theme-default/styles/theme.styl @@ -40,7 +40,7 @@ body display none .sidebar - font-size 15px + font-size 16px background-color #fff width $sidebarWidth position fixed diff --git a/packages/@vuepress/theme-default/util/index.js b/packages/@vuepress/theme-default/util/index.js index 01937e9888..1794b1f226 100644 --- a/packages/@vuepress/theme-default/util/index.js +++ b/packages/@vuepress/theme-default/util/index.js @@ -148,6 +148,7 @@ function resolveHeaders (page) { type: 'group', collapsable: false, title: page.title, + path: null, children: headers.map(h => ({ type: 'auto', title: h.title, @@ -207,7 +208,7 @@ function ensureEndingSlash (path) { : path + '/' } -function resolveItem (item, pages, base, isNested) { +function resolveItem (item, pages, base, groupDepth = 1) { if (typeof item === 'string') { return resolvePage(pages, item, base) } else if (Array.isArray(item)) { @@ -215,17 +216,18 @@ function resolveItem (item, pages, base, isNested) { title: item[1] }) } else { - if (isNested) { + if (groupDepth > 3) { console.error( - '[vuepress] Nested sidebar groups are not supported. ' + - 'Consider using navbar + categories instead.' + '[vuepress] detected a too deep nested sidebar group.' ) } const children = item.children || [] return { type: 'group', + path: item.path, title: item.title, - children: children.map(child => resolveItem(child, pages, base, true)), + sidebarDepth: item.sidebarDepth, + children: children.map(child => resolveItem(child, pages, base, groupDepth + 1)), collapsable: item.collapsable !== false } } diff --git a/packages/docs/docs/.vuepress/config.js b/packages/docs/docs/.vuepress/config.js index a3d43ae868..f12074bde8 100644 --- a/packages/docs/docs/.vuepress/config.js +++ b/packages/docs/docs/.vuepress/config.js @@ -155,6 +155,7 @@ function getThemeSidebar (groupA, introductionA) { { title: groupA, collapsable: false, + sidebarDepth: 2, children: [ ['', introductionA], 'using-a-theme', diff --git a/packages/docs/docs/theme/default-theme-config.md b/packages/docs/docs/theme/default-theme-config.md index e921d46ce2..818b1f6b6a 100644 --- a/packages/docs/docs/theme/default-theme-config.md +++ b/packages/docs/docs/theme/default-theme-config.md @@ -180,8 +180,10 @@ module.exports = { themeConfig: { sidebar: [ { - title: 'Group 1', - collapsable: false, + title: 'Group 1', // required + path: '/foo/' // optional, which should be a absolute path. + collapsable: false, // optional, defaults to true + sidebarDepth: 1, // optional, defaults to 1 children: [ '/' ] @@ -197,6 +199,12 @@ module.exports = { Sidebar groups are collapsable by default. You can force a group to be always open with `collapsable: false`. +A sidebar group config also supports [sidebarDepth](#nested-header-links) field to override the default sidebar depth (`1`). + +::: tip +   From `1.0.0-alpha-36` on, nested sidebar group is also supported, but the nesting depth should be less than 3, otherwise the console will receive a warning. +::: + ### Multiple Sidebars If you wish to display different sidebars for different sections of content, first organize your pages into directories for each desired section: diff --git a/packages/docs/docs/zh/theme/default-theme-config.md b/packages/docs/docs/zh/theme/default-theme-config.md index a7b36de431..6455c27f6f 100644 --- a/packages/docs/docs/zh/theme/default-theme-config.md +++ b/packages/docs/docs/zh/theme/default-theme-config.md @@ -177,8 +177,10 @@ module.exports = { themeConfig: { sidebar: [ { - title: 'Group 1', - collapsable: false, + title: 'Group 1', // 必要的 + path: '/foo/' // 可选的, 应该是一个绝对路径 + collapsable: false, // 可选的, 默认值是 true, + sidebarDepth: 1, // 可选的, 默认值是 1 children: [ '/' ] @@ -194,6 +196,12 @@ module.exports = { 侧边栏的每个子组默认是可折叠的,你可以设置 `collapsable: false` 来让一个组永远都是展开状态。 +一个侧边栏的子组配置同时支持 [sidebarDepth](#nested-header-links) 字段用于重写默认显示的侧边栏深度(`1`)。 + +::: tip + 从 `1.0.0-alpha-36` 开始,嵌套的侧边栏分组 也是支持的,但嵌套深度应小于 3,否则在控制台会收到警告。 +::: + ### 多个侧边栏 如果你想为不同的页面组来显示不同的侧边栏,首先,将你的页面文件组织成下述的目录结构: