fix: adjust multi sidebar matching logic

- matches by ensuring starting slash + startsWith
- catch-all fallback (`/`) should be placed at the end like VuePress
pull/198/head
Evan You 4 years ago
parent 4f0c90311f
commit 7e4b16ee52

@ -16,8 +16,8 @@ describe('client/theme-default/support/sideBar', () => {
it('gets the correct sidebar items from the given path', () => {
const sidebar = {
'/': [{ text: 'R', link: 'r' }],
'/guide/': [{ text: 'G', link: 'g' }]
'/guide/': [{ text: 'G', link: 'g' }],
'/': [{ text: 'R', link: 'r' }]
}
expect(getSideBarConfig(sidebar, '/')).toEqual(sidebar['/'])
@ -31,7 +31,8 @@ describe('client/theme-default/support/sideBar', () => {
}
expect(getSideBarConfig(s, '/guide/')).toEqual(s['/guide/'])
expect(getSideBarConfig(s, '/guide')).toEqual(s['/guide/'])
// no ending slash should not match
expect(getSideBarConfig(s, '/guide')).not.toEqual(s['/guide/'])
expect(getSideBarConfig(s, 'guide/')).toEqual(s['/guide/'])
expect(getSideBarConfig(s, 'guide/nested')).toEqual(s['/guide/'])
expect(getSideBarConfig(s, '/guide/nested')).toEqual(s['/guide/'])

@ -32,9 +32,9 @@ module.exports = {
],
sidebar: {
'/': getGuideSidebar(),
'/guide/': getGuideSidebar(),
'/config/': getConfigSidebar()
'/config/': getConfigSidebar(),
'/': getGuideSidebar()
}
}
}

@ -1,10 +1,5 @@
import { DefaultTheme } from '../config'
import {
isArray,
ensureSlash,
ensureStartingSlash,
removeExtention
} from '../utils'
import { isArray, ensureStartingSlash, removeExtention } from '../utils'
export function isSideBarConfig(
sidebar: DefaultTheme.SideBarConfig | DefaultTheme.MultiSideBarConfig
@ -32,15 +27,11 @@ export function getSideBarConfig(
return sidebar
}
// get the very first segment of the path to compare with multiple sidebar keys
// and make sure it's surrounded by slash
path = removeExtention(path)
path = ensureStartingSlash(path).split('/')[1] || '/'
path = ensureSlash(path)
path = ensureStartingSlash(path)
for (const dir in sidebar) {
// make sure the multi sidebar key is surrounded by slash too
if (path === ensureSlash(dir)) {
// make sure the multi sidebar key starts with slash too
if (path.startsWith(ensureStartingSlash(dir))) {
return sidebar[dir]
}
}

@ -17,7 +17,7 @@
"tests/*": ["__tests__/*"],
"/@shared/*": ["src/client/shared/*"],
"/@types/*": ["types/*"],
"vitepress": ["src/client/app/exports.ts"]
"vitepress": ["src/client/index.ts"]
}
},
"include": [

Loading…
Cancel
Save