Skip to content

Commit

Permalink
fix: not working with multi sidebar config
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking committed Nov 20, 2020
1 parent 07d8163 commit 49cb6fe
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
48 changes: 41 additions & 7 deletions src/client/theme-default/composables/nextAndPrevLinks.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { computed } from 'vue'
import { useSiteDataByRoute, usePageData } from 'vitepress'
import { isArray, getPathDirName, ensureStartingSlash } from '../utils'
import { DefaultTheme } from '../config'

export function useNextAndPrevLinks() {
const site = useSiteDataByRoute()
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(() => {
Expand Down Expand Up @@ -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<DefaultTheme.SideBarLink[]>((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
}
8 changes: 8 additions & 0 deletions src/client/theme-default/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/')
}
Expand Down Expand Up @@ -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}/`
}

0 comments on commit 49cb6fe

Please sign in to comment.