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:
Sébastien Lorber 2021-06-16 13:18:53 +02:00 committed by GitHub
parent 32e76f1cc0
commit aa176274be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 10 deletions

View file

@ -43,6 +43,11 @@ declare module '@docusaurus/plugin-content-docs-types' {
export type PropSidebars = {
[sidebarId: string]: PropSidebarItem[];
};
export type {
GlobalVersion as GlobalDataVersion,
GlobalDoc as GlobalDataDoc,
} from './types';
}
declare module '@theme/DocItem' {

View file

@ -11,6 +11,29 @@ import {useLatestVersion, useActiveDocContext} from '@theme/hooks/useDocs';
import clsx from 'clsx';
import type {Props} from '@theme/NavbarItem/DocNavbarItem';
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({
docId,
@ -23,16 +46,13 @@ export default function DocNavbarItem({
const {preferredVersion} = useDocsPreferredVersion(docsPluginId);
const latestVersion = useLatestVersion(docsPluginId);
const version = activeVersion ?? preferredVersion ?? latestVersion;
const doc = version.docs.find((versionDoc) => versionDoc.id === docId);
if (!doc) {
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}".
Available doc ids:\n- ${docIds}`,
);
}
// Versions used to look for the doc to link to, ordered + no duplicate
const versions: GlobalDataVersion[] = [
...new Set(
[activeVersion, preferredVersion, latestVersion].filter(Boolean),
),
];
const doc = getDocInVersions(versions, docId);
return (
<DefaultNavbarItem