From e1619c775ff0a34e847605248c69e75b23c8b87b Mon Sep 17 00:00:00 2001 From: yangxingyuan <39261479+sanyuan0704@users.noreply.github.com> Date: Tue, 2 Jan 2024 15:20:35 +0800 Subject: [PATCH] fix: doc sidebar when cleanUrls is enabled (#513) --- .changeset/smooth-pugs-kneel.md | 6 ++++++ packages/shared/src/runtime-utils/index.ts | 12 +++++++----- .../src/components/Nav/menuDataHooks.tsx | 4 ++++ 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 .changeset/smooth-pugs-kneel.md diff --git a/.changeset/smooth-pugs-kneel.md b/.changeset/smooth-pugs-kneel.md new file mode 100644 index 000000000..9312ff3ad --- /dev/null +++ b/.changeset/smooth-pugs-kneel.md @@ -0,0 +1,6 @@ +--- +'@rspress/theme-default': patch +'@rspress/shared': patch +--- + +fix: sidebar disappeared in cleanUrl mode diff --git a/packages/shared/src/runtime-utils/index.ts b/packages/shared/src/runtime-utils/index.ts index 84a0b9b6b..9d6e1d071 100644 --- a/packages/shared/src/runtime-utils/index.ts +++ b/packages/shared/src/runtime-utils/index.ts @@ -124,15 +124,16 @@ export function replaceLang( default: string; }, base = '', + cleanUrls = false, ) { let url = removeBase(rawUrl, base); // rspress.dev/builder + switch to en -> rspress.dev/builder/en/index.html if (!url) { - url = '/index.html'; + url = cleanUrls ? '/index' : '/index.html'; } if (url.endsWith('/')) { - url += 'index.html'; + url += cleanUrls ? '/index' : '/index.html'; } let versionPart = ''; @@ -159,7 +160,7 @@ export function replaceLang( purePathPart = parts.join('/') || ''; if ((versionPart || langPart) && !purePathPart) { - purePathPart = 'index.html'; + purePathPart = cleanUrls ? 'index' : 'index.html'; } return withBase( @@ -178,11 +179,12 @@ export function replaceVersion( default: string; }, base = '', + cleanUrls = false, ) { let url = removeBase(rawUrl, base); // rspress.dev/builder + switch to en -> rspress.dev/builder/en/index.html if (!url) { - url = '/index.html'; + url = cleanUrls ? '/index' : '/index.html'; } let versionPart = ''; @@ -200,7 +202,7 @@ export function replaceVersion( let restPart = parts.join('/') || ''; if (versionPart && !restPart) { - restPart = 'index.html'; + restPart = cleanUrls ? 'index' : 'index.html'; } return withBase( diff --git a/packages/theme-default/src/components/Nav/menuDataHooks.tsx b/packages/theme-default/src/components/Nav/menuDataHooks.tsx index 925bf10c7..041f803d9 100644 --- a/packages/theme-default/src/components/Nav/menuDataHooks.tsx +++ b/packages/theme-default/src/components/Nav/menuDataHooks.tsx @@ -11,6 +11,7 @@ export function useTranslationMenuData() { const localeLanguages = Object.values( siteData.locales || siteData.themeConfig.locales || {}, ); + const cleanUrls = siteData.route?.cleanUrls || false; const hasMultiLanguage = localeLanguages.length > 1; const { lang: currentLang } = page; const { base } = siteData; @@ -39,6 +40,7 @@ export function useTranslationMenuData() { default: defaultVersion, }, base, + cleanUrls, ), })), activeValue: localeLanguages.find(item => currentLang === item.lang) @@ -52,6 +54,7 @@ export function useVersionMenuData() { const { siteData } = usePageData(); const currentVersion = useVersion(); const { pathname } = useLocation(); + const cleanUrls = siteData.route?.cleanUrls || false; const defaultVersion = siteData.multiVersion.default || ''; const versions = siteData.multiVersion.versions || []; const { base } = siteData; @@ -66,6 +69,7 @@ export function useVersionMenuData() { default: defaultVersion, }, base, + cleanUrls, ), })), text: currentVersion,