From 7e4b16ee524efc87c150a3d57a3215aac76b3669 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 31 Dec 2020 11:04:20 -0500 Subject: [PATCH] fix: adjust multi sidebar matching logic - matches by ensuring starting slash + startsWith - catch-all fallback (`/`) should be placed at the end like VuePress --- .../theme-default/support/sideBar.spec.ts | 7 ++++--- docs/.vitepress/config.js | 4 ++-- src/client/theme-default/support/sideBar.ts | 17 ++++------------- tsconfig.json | 2 +- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/__tests__/client/theme-default/support/sideBar.spec.ts b/__tests__/client/theme-default/support/sideBar.spec.ts index a1d0ea825d01..033daced9983 100644 --- a/__tests__/client/theme-default/support/sideBar.spec.ts +++ b/__tests__/client/theme-default/support/sideBar.spec.ts @@ -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/']) diff --git a/docs/.vitepress/config.js b/docs/.vitepress/config.js index 0344926bb204..d9a95499fff5 100644 --- a/docs/.vitepress/config.js +++ b/docs/.vitepress/config.js @@ -32,9 +32,9 @@ module.exports = { ], sidebar: { - '/': getGuideSidebar(), '/guide/': getGuideSidebar(), - '/config/': getConfigSidebar() + '/config/': getConfigSidebar(), + '/': getGuideSidebar() } } } diff --git a/src/client/theme-default/support/sideBar.ts b/src/client/theme-default/support/sideBar.ts index c44d624ff773..7af837215a90 100644 --- a/src/client/theme-default/support/sideBar.ts +++ b/src/client/theme-default/support/sideBar.ts @@ -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] } } diff --git a/tsconfig.json b/tsconfig.json index 89e40b9b489f..c3cffe575010 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,7 +17,7 @@ "tests/*": ["__tests__/*"], "/@shared/*": ["src/client/shared/*"], "/@types/*": ["types/*"], - "vitepress": ["src/client/app/exports.ts"] + "vitepress": ["src/client/index.ts"] } }, "include": [