mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-11 08:07:26 +02:00
fix(v2): navbar doc item should allow older versions to not contain the target doc, as long as the lastVersion contains it (#4985)
This commit is contained in:
parent
32e76f1cc0
commit
aa176274be
2 changed files with 35 additions and 10 deletions
|
@ -43,6 +43,11 @@ declare module '@docusaurus/plugin-content-docs-types' {
|
||||||
export type PropSidebars = {
|
export type PropSidebars = {
|
||||||
[sidebarId: string]: PropSidebarItem[];
|
[sidebarId: string]: PropSidebarItem[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type {
|
||||||
|
GlobalVersion as GlobalDataVersion,
|
||||||
|
GlobalDoc as GlobalDataDoc,
|
||||||
|
} from './types';
|
||||||
}
|
}
|
||||||
|
|
||||||
declare module '@theme/DocItem' {
|
declare module '@theme/DocItem' {
|
||||||
|
|
|
@ -11,6 +11,29 @@ import {useLatestVersion, useActiveDocContext} from '@theme/hooks/useDocs';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import type {Props} from '@theme/NavbarItem/DocNavbarItem';
|
import type {Props} from '@theme/NavbarItem/DocNavbarItem';
|
||||||
import {useDocsPreferredVersion} from '@docusaurus/theme-common';
|
import {useDocsPreferredVersion} from '@docusaurus/theme-common';
|
||||||
|
import type {
|
||||||
|
GlobalDataVersion,
|
||||||
|
GlobalDataDoc,
|
||||||
|
} from '@docusaurus/plugin-content-docs-types';
|
||||||
|
|
||||||
|
function getDocInVersions(versions: GlobalDataVersion[], docId: string) {
|
||||||
|
// vanilla-js flatten, TODO replace soon by ES flat() / flatMap()
|
||||||
|
const allDocs: GlobalDataDoc[] = [].concat(
|
||||||
|
...versions.map((version) => version.docs),
|
||||||
|
);
|
||||||
|
|
||||||
|
const doc = allDocs.find((versionDoc) => versionDoc.id === docId);
|
||||||
|
if (!doc) {
|
||||||
|
const docIds = allDocs.map((versionDoc) => versionDoc.id).join('\n- ');
|
||||||
|
throw new Error(
|
||||||
|
`DocNavbarItem: couldn't find any doc with id "${docId}" in version${
|
||||||
|
versions.length ? 's' : ''
|
||||||
|
} ${versions.map((version) => version.name).join(', ')}".
|
||||||
|
Available doc ids are:\n- ${docIds}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return doc;
|
||||||
|
}
|
||||||
|
|
||||||
export default function DocNavbarItem({
|
export default function DocNavbarItem({
|
||||||
docId,
|
docId,
|
||||||
|
@ -23,16 +46,13 @@ export default function DocNavbarItem({
|
||||||
const {preferredVersion} = useDocsPreferredVersion(docsPluginId);
|
const {preferredVersion} = useDocsPreferredVersion(docsPluginId);
|
||||||
const latestVersion = useLatestVersion(docsPluginId);
|
const latestVersion = useLatestVersion(docsPluginId);
|
||||||
|
|
||||||
const version = activeVersion ?? preferredVersion ?? latestVersion;
|
// Versions used to look for the doc to link to, ordered + no duplicate
|
||||||
|
const versions: GlobalDataVersion[] = [
|
||||||
const doc = version.docs.find((versionDoc) => versionDoc.id === docId);
|
...new Set(
|
||||||
if (!doc) {
|
[activeVersion, preferredVersion, latestVersion].filter(Boolean),
|
||||||
const docIds = version.docs.map((versionDoc) => versionDoc.id).join('\n- ');
|
),
|
||||||
throw new Error(
|
];
|
||||||
`DocNavbarItem: couldn't find any doc with "${docId}" id in version "${version.name}".
|
const doc = getDocInVersions(versions, docId);
|
||||||
Available doc ids:\n- ${docIds}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DefaultNavbarItem
|
<DefaultNavbarItem
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue