fix: not working with multi sidebar config

pull/130/head
Kia Ishii 5 years ago
parent 07d8163649
commit 49cb6feb04

@ -1,5 +1,6 @@
import { computed } from 'vue' import { computed } from 'vue'
import { useSiteDataByRoute, usePageData } from 'vitepress' import { useSiteDataByRoute, usePageData } from 'vitepress'
import { isArray, getPathDirName, ensureStartingSlash } from '../utils'
import { DefaultTheme } from '../config' import { DefaultTheme } from '../config'
export function useNextAndPrevLinks() { export function useNextAndPrevLinks() {
@ -7,11 +8,17 @@ export function useNextAndPrevLinks() {
const page = usePageData() const page = usePageData()
const candidates = computed(() => { const candidates = computed(() => {
return getFlatSidebarLinks(site.value.themeConfig?.sidebar) const path = ensureStartingSlash(page.value.relativePath)
const sidebar = site.value.themeConfig?.sidebar
return getFlatSidebarLinks(path, sidebar)
}) })
const currentPath = computed(() => { const currentPath = computed(() => {
return '/' + page.value.relativePath.replace(/(index)?\.(md|html)$/, '') return ensureStartingSlash(page.value.relativePath).replace(
/(index)?\.(md|html)$/,
''
)
}) })
const currentIndex = computed(() => { const currentIndex = computed(() => {
@ -46,24 +53,51 @@ export function useNextAndPrevLinks() {
} }
function getFlatSidebarLinks( function getFlatSidebarLinks(
path: string,
sidebar?: DefaultTheme.SideBarConfig sidebar?: DefaultTheme.SideBarConfig
): DefaultTheme.SideBarLink[] { ): DefaultTheme.SideBarLink[] {
if (!sidebar || sidebar === 'auto') { if (!sidebar || sidebar === 'auto') {
return [] return []
} }
return isArray(sidebar)
? getFlatSidebarLinksFromArray(path, sidebar)
: getFlatSidebarLinksFromObject(path, sidebar)
}
function getFlatSidebarLinksFromArray(
path: string,
sidebar: DefaultTheme.SideBarItem[]
): DefaultTheme.SideBarLink[] {
return sidebar.reduce<DefaultTheme.SideBarLink[]>((links, item) => { return sidebar.reduce<DefaultTheme.SideBarLink[]>((links, item) => {
if (item.link) { if (item.link) {
links.push({ text: item.text, link: item.link }) links.push({ text: item.text, link: item.link })
} }
if ((item as DefaultTheme.SideBarGroup).children) { if (isSideBarGroup(item)) {
links = [ links = [...links, ...getFlatSidebarLinks(path, item.children)]
...links,
...getFlatSidebarLinks((item as DefaultTheme.SideBarGroup).children)
]
} }
return links return links
}, []) }, [])
} }
function getFlatSidebarLinksFromObject(
path: string,
sidebar: DefaultTheme.MultiSideBarConfig
): DefaultTheme.SideBarLink[] {
const paths = [path, Object.keys(sidebar)[0]]
const item = paths.map((p) => sidebar[getPathDirName(p)]).find(Boolean)
if (isArray(item)) {
return getFlatSidebarLinksFromArray(path, item)
}
return []
}
function isSideBarGroup(
item: DefaultTheme.SideBarItem
): item is DefaultTheme.SideBarGroup {
return (item as DefaultTheme.SideBarGroup).children !== undefined
}

@ -9,6 +9,10 @@ export function isNullish(value: any): value is null | undefined {
return value === null || value === undefined return value === null || value === undefined
} }
export function isArray(value: any): value is any[] {
return Array.isArray(value)
}
export function withBase(path: string) { export function withBase(path: string) {
return (useSiteData().value.base + path).replace(/\/+/g, '/') return (useSiteData().value.base + path).replace(/\/+/g, '/')
} }
@ -62,6 +66,10 @@ export function getPathDirName(path: string): string {
return ensureEndingSlash(segments.join('/')) return ensureEndingSlash(segments.join('/'))
} }
export function ensureStartingSlash(path: string): string {
return /^\//.test(path) ? path : `/${path}`
}
export function ensureEndingSlash(path: string): string { export function ensureEndingSlash(path: string): string {
return /(\.html|\/)$/.test(path) ? path : `${path}/` return /(\.html|\/)$/.test(path) ? path : `${path}/`
} }

Loading…
Cancel
Save