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(v2): navbar doc item fallback: search doc in lastVersion #4985

Merged
merged 1 commit into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
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 @@ -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' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down