Skip to content

Commit

Permalink
make findFirstLink receive a LoadedVersion object to find a doc's per…
Browse files Browse the repository at this point in the history
…malink from within the function
  • Loading branch information
lmpham1 committed Dec 30, 2021
1 parent 1c7c848 commit 3192dcb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 40 deletions.
16 changes: 2 additions & 14 deletions packages/docusaurus-plugin-content-docs/src/globalData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import {mapValues} from 'lodash';
import {normalizeUrl} from '@docusaurus/utils';
import type {Sidebars} from './sidebars/types';
import {createSidebarsUtils} from './sidebars/utils';
import type {
Expand All @@ -31,20 +30,9 @@ export function toGlobalSidebars(
): Record<string, GlobalSidebar> {
const {findFirstLink} = createSidebarsUtils(sidebars);
return mapValues(sidebars, (sidebar, sidebarId) => {
const firstDoc = findFirstLink(sidebarId);
if (!firstDoc) {
return {};
}
const firstDoc = findFirstLink(sidebarId, version);
return {
link: {
path: firstDoc.isAutoGeneratedIndex
? normalizeUrl([version.versionPath, firstDoc.slug])
: version.docs.find(
(doc) =>
doc.id === firstDoc.id || doc.unversionedId === firstDoc.id,
)!.permalink,
label: firstDoc.label,
},
link: firstDoc,
};
});
}
Expand Down
54 changes: 28 additions & 26 deletions packages/docusaurus-plugin-content-docs/src/sidebars/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ import type {
} from './types';

import {mapValues, difference, uniq} from 'lodash';
import {getElementsAround, toMessageRelativeFilePath} from '@docusaurus/utils';
import {DocMetadataBase, DocNavLink} from '../types';
import {
getElementsAround,
normalizeUrl,
toMessageRelativeFilePath,
} from '@docusaurus/utils';
import {DocMetadataBase, DocNavLink, LoadedVersion} from '../types';
import {
SidebarItemCategoryWithGeneratedIndex,
SidebarItemCategoryWithLink,
Expand Down Expand Up @@ -138,15 +142,12 @@ export type SidebarsUtils = {
getCategoryGeneratedIndexNavigation: (
categoryGeneratedIndexPermalink: string,
) => SidebarNavigation;
findFirstLink: (sidebarId: string) =>
| {
isAutoGeneratedIndex: false;
id: string;
label: string;
}
findFirstLink: (
sidebarId: string,
version: LoadedVersion,
) =>
| {
isAutoGeneratedIndex: true;
slug: string;
path: string;
label: string;
}
| undefined;
Expand Down Expand Up @@ -278,41 +279,42 @@ Available document ids are:
}
}

function findFirstLink(sidebar: Sidebar):
| {
isAutoGeneratedIndex: false;
id: string;
label: string;
}
function findFirstLink(
sidebar: Sidebar,
version: LoadedVersion,
):
| {
isAutoGeneratedIndex: true;
slug: string;
label: string;
path: string;
}
| undefined {
// eslint-disable-next-line no-restricted-syntax
for (const item of sidebar) {
if (item.type === 'doc') {
return {
isAutoGeneratedIndex: false,
id: item.id,
path: version.docs.find(
(doc) => doc.id === item.id || doc.unversionedId === item.id,
)!.permalink,
label: item.label ?? item.id,
};
} else if (item.type === 'category') {
if (item.link?.type === 'doc') {
const categoryLinkId = item.link.id;
return {
isAutoGeneratedIndex: false,
id: item.link.id,
path: version.docs.find(
(doc) =>
doc.id === categoryLinkId ||
doc.unversionedId === categoryLinkId,
)!.permalink,
label: item.label,
};
} else if (item.link?.type === 'generated-index') {
return {
isAutoGeneratedIndex: true,
slug: item.link.slug,
path: normalizeUrl([version.versionPath, item.link.slug]),
label: item.label,
};
} else {
const doc = findFirstLink(item.items);
const doc = findFirstLink(item.items, version);
if (doc) {
return doc;
}
Expand All @@ -330,7 +332,7 @@ Available document ids are:
getCategoryGeneratedIndexList,
getCategoryGeneratedIndexNavigation,
checkSidebarsDocIds,
findFirstLink: (id) => findFirstLink(sidebars[id]),
findFirstLink: (id, version) => findFirstLink(sidebars[id], version),
};
}

Expand Down

0 comments on commit 3192dcb

Please sign in to comment.