Skip to content

Commit 5a2220b

Browse files
slorberOzakIOne
authored andcommitted
fix(theme): fix DocsVersionDropdownNavbarItem version link target (#10288)
1 parent 21ab58d commit 5a2220b

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionDropdownNavbarItem.tsx

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,28 @@ import {useLocation} from '@docusaurus/router';
1717
import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem';
1818
import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem';
1919
import type {Props} from '@theme/NavbarItem/DocsVersionDropdownNavbarItem';
20-
import type {GlobalVersion} from '@docusaurus/plugin-content-docs/client';
20+
import type {LinkLikeNavbarItemProps} from '@theme/NavbarItem';
21+
import type {
22+
GlobalVersion,
23+
GlobalDoc,
24+
ActiveDocContext,
25+
} from '@docusaurus/plugin-content-docs/client';
2126

22-
const getVersionMainDoc = (version: GlobalVersion) =>
23-
version.docs.find((doc) => doc.id === version.mainDocId)!;
27+
function getVersionMainDoc(version: GlobalVersion): GlobalDoc {
28+
return version.docs.find((doc) => doc.id === version.mainDocId)!;
29+
}
30+
31+
function getVersionTargetDoc(
32+
version: GlobalVersion,
33+
activeDocContext: ActiveDocContext,
34+
): GlobalDoc {
35+
// We try to link to the same doc, in another version
36+
// When not possible, fallback to the "main doc" of the version
37+
return (
38+
activeDocContext.alternateDocVersions[version.name] ??
39+
getVersionMainDoc(version)
40+
);
41+
}
2442

2543
export default function DocsVersionDropdownNavbarItem({
2644
mobile,
@@ -34,23 +52,21 @@ export default function DocsVersionDropdownNavbarItem({
3452
const activeDocContext = useActiveDocContext(docsPluginId);
3553
const versions = useVersions(docsPluginId);
3654
const {savePreferredVersionName} = useDocsPreferredVersion(docsPluginId);
37-
const versionLinks = versions.map((version) => {
38-
// We try to link to the same doc, in another version
39-
// When not possible, fallback to the "main doc" of the version
40-
const versionDoc =
41-
activeDocContext.alternateDocVersions[version.name] ??
42-
getVersionMainDoc(version);
55+
56+
function versionToLink(version: GlobalVersion): LinkLikeNavbarItemProps {
57+
const targetDoc = getVersionTargetDoc(version, activeDocContext);
4358
return {
4459
label: version.label,
4560
// preserve ?search#hash suffix on version switches
46-
to: `${versionDoc.path}${search}${hash}`,
61+
to: `${targetDoc.path}${search}${hash}`,
4762
isActive: () => version === activeDocContext.activeVersion,
4863
onClick: () => savePreferredVersionName(version.name),
4964
};
50-
});
51-
const items = [
65+
}
66+
67+
const items: LinkLikeNavbarItemProps[] = [
5268
...dropdownItemsBefore,
53-
...versionLinks,
69+
...versions.map(versionToLink),
5470
...dropdownItemsAfter,
5571
];
5672

@@ -69,7 +85,7 @@ export default function DocsVersionDropdownNavbarItem({
6985
const dropdownTo =
7086
mobile && items.length > 1
7187
? undefined
72-
: getVersionMainDoc(dropdownVersion).path;
88+
: getVersionTargetDoc(dropdownVersion, activeDocContext).path;
7389

7490
// We don't want to render a version dropdown with 0 or 1 item. If we build
7591
// the site with a single docs version (onlyIncludeVersions: ['1.0.0']),

0 commit comments

Comments
 (0)