Skip to content

Commit

Permalink
fix(theme): fix DocsVersionDropdownNavbarItem version link target (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber authored and OzakIOne committed Jul 11, 2024
1 parent 9da3448 commit 3b918fd
Showing 1 changed file with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
];

Expand All @@ -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']),
Expand Down

0 comments on commit 3b918fd

Please sign in to comment.