From 49cb6feb04a9e2bdc8b77d023626634f07ff8423 Mon Sep 17 00:00:00 2001 From: Kia Ishii Date: Fri, 20 Nov 2020 16:48:26 +0900 Subject: [PATCH] fix: not working with multi sidebar config --- .../composables/nextAndPrevLinks.ts | 48 ++++++++++++++++--- src/client/theme-default/utils.ts | 8 ++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/client/theme-default/composables/nextAndPrevLinks.ts b/src/client/theme-default/composables/nextAndPrevLinks.ts index f3895fcd3845..f65743a98c74 100644 --- a/src/client/theme-default/composables/nextAndPrevLinks.ts +++ b/src/client/theme-default/composables/nextAndPrevLinks.ts @@ -1,5 +1,6 @@ import { computed } from 'vue' import { useSiteDataByRoute, usePageData } from 'vitepress' +import { isArray, getPathDirName, ensureStartingSlash } from '../utils' import { DefaultTheme } from '../config' export function useNextAndPrevLinks() { @@ -7,11 +8,17 @@ export function useNextAndPrevLinks() { const page = usePageData() 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(() => { - return '/' + page.value.relativePath.replace(/(index)?\.(md|html)$/, '') + return ensureStartingSlash(page.value.relativePath).replace( + /(index)?\.(md|html)$/, + '' + ) }) const currentIndex = computed(() => { @@ -46,24 +53,51 @@ export function useNextAndPrevLinks() { } function getFlatSidebarLinks( + path: string, sidebar?: DefaultTheme.SideBarConfig ): DefaultTheme.SideBarLink[] { if (!sidebar || sidebar === 'auto') { return [] } + return isArray(sidebar) + ? getFlatSidebarLinksFromArray(path, sidebar) + : getFlatSidebarLinksFromObject(path, sidebar) +} + +function getFlatSidebarLinksFromArray( + path: string, + sidebar: DefaultTheme.SideBarItem[] +): DefaultTheme.SideBarLink[] { return sidebar.reduce((links, item) => { if (item.link) { links.push({ text: item.text, link: item.link }) } - if ((item as DefaultTheme.SideBarGroup).children) { - links = [ - ...links, - ...getFlatSidebarLinks((item as DefaultTheme.SideBarGroup).children) - ] + if (isSideBarGroup(item)) { + links = [...links, ...getFlatSidebarLinks(path, item.children)] } 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 +} diff --git a/src/client/theme-default/utils.ts b/src/client/theme-default/utils.ts index 0ff92f237f91..2f8b4bd44566 100644 --- a/src/client/theme-default/utils.ts +++ b/src/client/theme-default/utils.ts @@ -9,6 +9,10 @@ export function isNullish(value: any): value is null | undefined { return value === null || value === undefined } +export function isArray(value: any): value is any[] { + return Array.isArray(value) +} + export function withBase(path: string) { return (useSiteData().value.base + path).replace(/\/+/g, '/') } @@ -62,6 +66,10 @@ export function getPathDirName(path: string): string { return ensureEndingSlash(segments.join('/')) } +export function ensureStartingSlash(path: string): string { + return /^\//.test(path) ? path : `/${path}` +} + export function ensureEndingSlash(path: string): string { return /(\.html|\/)$/.test(path) ? path : `${path}/` }