Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(theme): fix DocsVersionDropdownNavbarItem version link target #10288

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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