Skip to content

Commit 050fa4c

Browse files
authoredJul 21, 2020
feat: add multi sidebar support (#38) (#49)
* feat: add multi sidebar support * refactor: rename `resolvePath` to more readable one
1 parent 5780461 commit 050fa4c

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed
 

Diff for: ‎src/client/theme-default/components/SideBar.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useSiteData, usePageData, useRoute } from 'vitepress'
22
import { computed, h, FunctionalComponent, VNode } from 'vue'
33
import { Header } from '../../../../types/shared'
4-
import { isActive } from '../utils'
4+
import { isActive, getPathDirName } from '../utils'
55
import { DefaultTheme } from '../config'
66
import { useActiveSidebarLinks } from '../composables/activeSidebarLink'
77

@@ -62,7 +62,12 @@ export default {
6262
} else if (themeSidebar === false) {
6363
return []
6464
} else if (typeof themeSidebar === 'object') {
65-
return resolveMultiSidebar(themeSidebar, route.path, sidebarDepth)
65+
return resolveMultiSidebar(
66+
themeSidebar,
67+
route.path,
68+
headers,
69+
sidebarDepth
70+
)
6671
}
6772
}
6873
})
@@ -86,6 +91,10 @@ interface ResolvedSidebarItem {
8691
function resolveAutoSidebar(headers: Header[], depth: number): ResolvedSidebar {
8792
const ret: ResolvedSidebar = []
8893

94+
if (headers === undefined) {
95+
return []
96+
}
97+
8998
let lastH2: ResolvedSidebarItem | undefined = undefined
9099
headers.forEach(({ level, title, slug }) => {
91100
if (level - 1 > depth) {
@@ -117,8 +126,19 @@ function resolveArraySidebar(
117126
function resolveMultiSidebar(
118127
config: DefaultTheme.MultiSideBarConfig,
119128
path: string,
129+
headers: Header[],
120130
depth: number
121131
): ResolvedSidebar {
132+
const item = config[getPathDirName(path)]
133+
134+
if (Array.isArray(item)) {
135+
return resolveArraySidebar(item, depth)
136+
}
137+
138+
if (item === 'auto') {
139+
return resolveAutoSidebar(headers, depth)
140+
}
141+
122142
return []
123143
}
124144

Diff for: ‎src/client/theme-default/utils.ts

+19
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,22 @@ export function isActive(route: Route, path?: string): boolean {
2626
export function normalize(path: string): string {
2727
return decodeURI(path).replace(hashRE, '').replace(extRE, '')
2828
}
29+
30+
/**
31+
* get the path without filename (the last segment). for example, if the given
32+
* path is `/guide/getting-started.html`, this method will return `/guide/`.
33+
* Always with a trailing slash.
34+
*/
35+
export function getPathDirName(path: string): string {
36+
const segments = path.split('/')
37+
38+
if (segments[segments.length - 1]) {
39+
segments.pop()
40+
}
41+
42+
return ensureEndingSlash(segments.join('/'))
43+
}
44+
45+
export function ensureEndingSlash(path: string): string {
46+
return /(\.html|\/)$/.test(path) ? path : `${path}/`
47+
}

0 commit comments

Comments
 (0)