mirror of
https://github.com/facebook/docusaurus.git
synced 2025-12-26 01:33:02 +00:00
fix(theme): fix DocsVersionDropdownNavbarItem version link target (#10288)
This commit is contained in:
parent
80203b385d
commit
8b877d27d4
|
|
@ -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']),
|
||||
|
|
|
|||
Loading…
Reference in New Issue