From 5a2220bf4271a01999c4325f204f81796a4d7134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lorber?= Date: Wed, 10 Jul 2024 18:22:55 +0200 Subject: [PATCH] fix(theme): fix DocsVersionDropdownNavbarItem version link target (#10288) --- .../DocsVersionDropdownNavbarItem.tsx | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionDropdownNavbarItem.tsx b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionDropdownNavbarItem.tsx index b6ef8e95f229c..6e67d938954d5 100644 --- a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionDropdownNavbarItem.tsx +++ b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionDropdownNavbarItem.tsx @@ -17,10 +17,28 @@ import {useLocation} from '@docusaurus/router'; import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem'; import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem'; import type {Props} from '@theme/NavbarItem/DocsVersionDropdownNavbarItem'; -import type {GlobalVersion} from '@docusaurus/plugin-content-docs/client'; +import type {LinkLikeNavbarItemProps} from '@theme/NavbarItem'; +import type { + GlobalVersion, + GlobalDoc, + ActiveDocContext, +} from '@docusaurus/plugin-content-docs/client'; -const getVersionMainDoc = (version: GlobalVersion) => - version.docs.find((doc) => doc.id === version.mainDocId)!; +function getVersionMainDoc(version: GlobalVersion): GlobalDoc { + return version.docs.find((doc) => doc.id === version.mainDocId)!; +} + +function getVersionTargetDoc( + version: GlobalVersion, + activeDocContext: ActiveDocContext, +): GlobalDoc { + // We try to link to the same doc, in another version + // When not possible, fallback to the "main doc" of the version + return ( + activeDocContext.alternateDocVersions[version.name] ?? + getVersionMainDoc(version) + ); +} export default function DocsVersionDropdownNavbarItem({ mobile, @@ -34,23 +52,21 @@ export default function DocsVersionDropdownNavbarItem({ const activeDocContext = useActiveDocContext(docsPluginId); const versions = useVersions(docsPluginId); const {savePreferredVersionName} = useDocsPreferredVersion(docsPluginId); - const versionLinks = versions.map((version) => { - // We try to link to the same doc, in another version - // When not possible, fallback to the "main doc" of the version - const versionDoc = - activeDocContext.alternateDocVersions[version.name] ?? - getVersionMainDoc(version); + + function versionToLink(version: GlobalVersion): LinkLikeNavbarItemProps { + const targetDoc = getVersionTargetDoc(version, activeDocContext); return { label: version.label, // preserve ?search#hash suffix on version switches - to: `${versionDoc.path}${search}${hash}`, + to: `${targetDoc.path}${search}${hash}`, isActive: () => version === activeDocContext.activeVersion, onClick: () => savePreferredVersionName(version.name), }; - }); - const items = [ + } + + const items: LinkLikeNavbarItemProps[] = [ ...dropdownItemsBefore, - ...versionLinks, + ...versions.map(versionToLink), ...dropdownItemsAfter, ]; @@ -69,7 +85,7 @@ export default function DocsVersionDropdownNavbarItem({ const dropdownTo = mobile && items.length > 1 ? undefined - : getVersionMainDoc(dropdownVersion).path; + : getVersionTargetDoc(dropdownVersion, activeDocContext).path; // We don't want to render a version dropdown with 0 or 1 item. If we build // the site with a single docs version (onlyIncludeVersions: ['1.0.0']),