Skip to content

Commit

Permalink
fix: adjust multi sidebar matching logic
Browse files Browse the repository at this point in the history
- matches by ensuring starting slash + startsWith
- catch-all fallback (`/`) should be placed at the end like VuePress
  • Loading branch information
yyx990803 committed Dec 31, 2020
1 parent 4f0c903 commit 7e4b16e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
7 changes: 4 additions & 3 deletions __tests__/client/theme-default/support/sideBar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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['/'])
Expand All @@ -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/'])
Expand Down
4 changes: 2 additions & 2 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ module.exports = {
],

sidebar: {
'/': getGuideSidebar(),
'/guide/': getGuideSidebar(),
'/config/': getConfigSidebar()
'/config/': getConfigSidebar(),
'/': getGuideSidebar()
}
}
}
Expand Down
17 changes: 4 additions & 13 deletions src/client/theme-default/support/sideBar.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]
}
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"tests/*": ["__tests__/*"],
"/@shared/*": ["src/client/shared/*"],
"/@types/*": ["types/*"],
"vitepress": ["src/client/app/exports.ts"]
"vitepress": ["src/client/index.ts"]
}
},
"include": [
Expand Down

0 comments on commit 7e4b16e

Please sign in to comment.