From 3de77e5c5fe479a4c310b66e85240d87d1abf68c Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Tue, 15 Jul 2025 16:12:49 +0800 Subject: [PATCH] fix(theme): fix getSidebar's buggy logic when supporting subtree-lifting hierachical sidebar config close #4841 --- src/client/theme-default/support/sidebar.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/client/theme-default/support/sidebar.ts b/src/client/theme-default/support/sidebar.ts index 13cd0aad..8a45bc15 100644 --- a/src/client/theme-default/support/sidebar.ts +++ b/src/client/theme-default/support/sidebar.ts @@ -27,11 +27,18 @@ export function getSidebar( const dir = Object.keys(_sidebar) .sort((a, b) => { + // longer dir link has higher priority return b.split('/').length - a.split('/').length }) .find((dir) => { + const dirWithStartingSlash = ensureStartingSlash(dir) // make sure the multi sidebar key starts with slash too - return path.startsWith(ensureStartingSlash(dir)) + if (path.startsWith(dirWithStartingSlash)) { + // "/" match everything and it has lowest priority + if (dirWithStartingSlash === '/') return true + const remains = path.replace(dirWithStartingSlash, '') + if (remains.startsWith('/') || remains === '') return true + } }) const sidebar = dir ? _sidebar[dir] : []