Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add multi sidebar support (#38) #49

Merged
merged 2 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/client/theme-default/components/SideBar.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useSiteData, usePageData, useRoute } from 'vitepress'
import { computed, h, FunctionalComponent, VNode } from 'vue'
import { Header } from '../../../../types/shared'
import { isActive } from '../utils'
import { isActive, getPathDirName } from '../utils'
import { DefaultTheme } from '../config'
import { useActiveSidebarLinks } from '../composables/activeSidebarLink'

Expand Down Expand Up @@ -62,7 +62,12 @@ export default {
} else if (themeSidebar === false) {
return []
} else if (typeof themeSidebar === 'object') {
return resolveMultiSidebar(themeSidebar, route.path, sidebarDepth)
return resolveMultiSidebar(
themeSidebar,
route.path,
headers,
sidebarDepth
)
}
}
})
Expand All @@ -86,6 +91,10 @@ interface ResolvedSidebarItem {
function resolveAutoSidebar(headers: Header[], depth: number): ResolvedSidebar {
const ret: ResolvedSidebar = []

if (headers === undefined) {
return []
}

let lastH2: ResolvedSidebarItem | undefined = undefined
headers.forEach(({ level, title, slug }) => {
if (level - 1 > depth) {
Expand Down Expand Up @@ -117,8 +126,19 @@ function resolveArraySidebar(
function resolveMultiSidebar(
config: DefaultTheme.MultiSideBarConfig,
path: string,
headers: Header[],
depth: number
): ResolvedSidebar {
const item = config[getPathDirName(path)]

if (Array.isArray(item)) {
return resolveArraySidebar(item, depth)
}

if (item === 'auto') {
return resolveAutoSidebar(headers, depth)
}

return []
}

Expand Down
19 changes: 19 additions & 0 deletions src/client/theme-default/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,22 @@ export function isActive(route: Route, path?: string): boolean {
export function normalize(path: string): string {
return decodeURI(path).replace(hashRE, '').replace(extRE, '')
}

/**
* get the path without filename (the last segment). for example, if the given
* path is `/guide/getting-started.html`, this method will return `/guide/`.
* Always with a trailing slash.
*/
export function getPathDirName(path: string): string {
const segments = path.split('/')

if (segments[segments.length - 1]) {
segments.pop()
}

return ensureEndingSlash(segments.join('/'))
}

export function ensureEndingSlash(path: string): string {
return /(\.html|\/)$/.test(path) ? path : `${path}/`
}