Skip to content

Commit 7e4b16e

Browse files
committed
fix: adjust multi sidebar matching logic
- matches by ensuring starting slash + startsWith - catch-all fallback (`/`) should be placed at the end like VuePress
1 parent 4f0c903 commit 7e4b16e

File tree

4 files changed

+11
-19
lines changed

4 files changed

+11
-19
lines changed

__tests__/client/theme-default/support/sideBar.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ describe('client/theme-default/support/sideBar', () => {
1616

1717
it('gets the correct sidebar items from the given path', () => {
1818
const sidebar = {
19-
'/': [{ text: 'R', link: 'r' }],
20-
'/guide/': [{ text: 'G', link: 'g' }]
19+
'/guide/': [{ text: 'G', link: 'g' }],
20+
'/': [{ text: 'R', link: 'r' }]
2121
}
2222

2323
expect(getSideBarConfig(sidebar, '/')).toEqual(sidebar['/'])
@@ -31,7 +31,8 @@ describe('client/theme-default/support/sideBar', () => {
3131
}
3232

3333
expect(getSideBarConfig(s, '/guide/')).toEqual(s['/guide/'])
34-
expect(getSideBarConfig(s, '/guide')).toEqual(s['/guide/'])
34+
// no ending slash should not match
35+
expect(getSideBarConfig(s, '/guide')).not.toEqual(s['/guide/'])
3536
expect(getSideBarConfig(s, 'guide/')).toEqual(s['/guide/'])
3637
expect(getSideBarConfig(s, 'guide/nested')).toEqual(s['/guide/'])
3738
expect(getSideBarConfig(s, '/guide/nested')).toEqual(s['/guide/'])

docs/.vitepress/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ module.exports = {
3232
],
3333

3434
sidebar: {
35-
'/': getGuideSidebar(),
3635
'/guide/': getGuideSidebar(),
37-
'/config/': getConfigSidebar()
36+
'/config/': getConfigSidebar(),
37+
'/': getGuideSidebar()
3838
}
3939
}
4040
}

src/client/theme-default/support/sideBar.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { DefaultTheme } from '../config'
2-
import {
3-
isArray,
4-
ensureSlash,
5-
ensureStartingSlash,
6-
removeExtention
7-
} from '../utils'
2+
import { isArray, ensureStartingSlash, removeExtention } from '../utils'
83

94
export function isSideBarConfig(
105
sidebar: DefaultTheme.SideBarConfig | DefaultTheme.MultiSideBarConfig
@@ -32,15 +27,11 @@ export function getSideBarConfig(
3227
return sidebar
3328
}
3429

35-
// get the very first segment of the path to compare with multiple sidebar keys
36-
// and make sure it's surrounded by slash
37-
path = removeExtention(path)
38-
path = ensureStartingSlash(path).split('/')[1] || '/'
39-
path = ensureSlash(path)
30+
path = ensureStartingSlash(path)
4031

4132
for (const dir in sidebar) {
42-
// make sure the multi sidebar key is surrounded by slash too
43-
if (path === ensureSlash(dir)) {
33+
// make sure the multi sidebar key starts with slash too
34+
if (path.startsWith(ensureStartingSlash(dir))) {
4435
return sidebar[dir]
4536
}
4637
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"tests/*": ["__tests__/*"],
1818
"/@shared/*": ["src/client/shared/*"],
1919
"/@types/*": ["types/*"],
20-
"vitepress": ["src/client/app/exports.ts"]
20+
"vitepress": ["src/client/index.ts"]
2121
}
2222
},
2323
"include": [

0 commit comments

Comments
 (0)