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

feat(theme-classic): new navbar item linking to a sidebar #6139

Merged
merged 26 commits into from
Jan 6, 2022
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
54f1468
<feat>(content-docs): added GlobalSidebar, GlobalSidebarLink, toGloba…
lmpham1 Dec 19, 2021
1f71b22
<feat>(theme-classic): added DocSidebarNavbarItem as the new NavbarIt…
lmpham1 Dec 19, 2021
99f52a3
<feat>(theme-classic, content-doc): normalized doc id into URL in plu…
lmpham1 Dec 20, 2021
e75ab58
<feat>(content-docs): fixed a bug in getFirstDocIdOfSidebar function …
lmpham1 Dec 21, 2021
7a6813b
Merge branch 'main' into navbar-sidebar-first-doc
Josh-Cena Dec 21, 2021
4c9015b
Various refactors
Josh-Cena Dec 21, 2021
1b30b5a
id => sidebarId
Josh-Cena Dec 21, 2021
e41a956
Link should always be defined
Josh-Cena Dec 21, 2021
8d79727
Merge branch 'main' into navbar-sidebar-first-doc
Josh-Cena Dec 21, 2021
3e3c280
Fallback to sidebar ID
Josh-Cena Dec 21, 2021
939d0a3
Added unit tests for getFirstDocOfSidebar
lmpham1 Dec 21, 2021
5941f7b
added documentation for the new DocSidebarNavbarItem type
lmpham1 Dec 22, 2021
8b926c6
Update theme-configuration.md
Josh-Cena Dec 22, 2021
00bc1ac
Merge branch 'main' into navbar-sidebar-first-doc
slorber Dec 28, 2021
95bf5ea
Update packages/docusaurus-theme-classic/src/theme/NavbarItem/DocSide…
lmpham1 Dec 30, 2021
04e70b4
changed getSidebarInVersion function in the DocSidebarNavbarItem modu…
lmpham1 Dec 30, 2021
1c7c848
refactor findFirstLink and toGlobalSidebars functions in doc plugin, …
lmpham1 Dec 30, 2021
3192dcb
make findFirstLink receive a LoadedVersion object to find a doc's per…
lmpham1 Dec 30, 2021
2ae7dfe
Merge branch 'main' into navbar-sidebar-first-doc
Josh-Cena Jan 2, 2022
95643c8
Revert "make findFirstLink receive a LoadedVersion object to find a d…
Josh-Cena Jan 2, 2022
ae7be3a
fix tests
Josh-Cena Jan 2, 2022
794ac8c
Lift type
Josh-Cena Jan 2, 2022
04c196a
Update packages/docusaurus-plugin-content-docs/src/globalData.ts
Josh-Cena Jan 5, 2022
ec7dd46
fixes
Josh-Cena Jan 6, 2022
a2ad091
Fix docs
Josh-Cena Jan 6, 2022
bcd01a9
Update utils.ts
Josh-Cena Jan 6, 2022
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
Prev Previous commit
Next Next commit
Revert "make findFirstLink receive a LoadedVersion object to find a d…
…oc's permalink from within the function"

This reverts commit 3192dcb.

Signed-off-by: Joshua Chen <[email protected]>
Josh-Cena committed Jan 2, 2022
commit 95643c89dbbc69ff3a3a403a7532b71f4bc454b9
16 changes: 14 additions & 2 deletions packages/docusaurus-plugin-content-docs/src/globalData.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
*/

import {mapValues} from 'lodash';
import {normalizeUrl} from '@docusaurus/utils';
import type {Sidebars} from './sidebars/types';
import {createSidebarsUtils} from './sidebars/utils';
import type {
@@ -30,9 +31,20 @@ export function toGlobalSidebars(
): Record<string, GlobalSidebar> {
const {findFirstLink} = createSidebarsUtils(sidebars);
return mapValues(sidebars, (sidebar, sidebarId) => {
const firstDoc = findFirstLink(sidebarId, version);
const firstDoc = findFirstLink(sidebarId);
if (!firstDoc) {
return {};
}
return {
link: firstDoc,
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,
},
};
});
}
54 changes: 26 additions & 28 deletions packages/docusaurus-plugin-content-docs/src/sidebars/utils.ts
Original file line number Diff line number Diff line change
@@ -21,12 +21,8 @@ import type {
} from './types';

import {mapValues, difference, uniq} from 'lodash';
import {
getElementsAround,
normalizeUrl,
toMessageRelativeFilePath,
} from '@docusaurus/utils';
import type {DocMetadataBase, DocNavLink, LoadedVersion} from '../types';
import {getElementsAround, toMessageRelativeFilePath} from '@docusaurus/utils';
import type {DocMetadataBase, DocNavLink} from '../types';

export function isCategoriesShorthand(
item: SidebarItemConfig,
@@ -140,12 +136,15 @@ export type SidebarsUtils = {
getCategoryGeneratedIndexNavigation: (
categoryGeneratedIndexPermalink: string,
) => SidebarNavigation;
findFirstLink: (
sidebarId: string,
version: LoadedVersion,
) =>
findFirstLink: (sidebarId: string) =>
| {
path: string;
isAutoGeneratedIndex: false;
id: string;
label: string;
}
| {
isAutoGeneratedIndex: true;
slug: string;
label: string;
}
| undefined;
@@ -277,42 +276,41 @@ Available document ids are:
}
}

function findFirstLink(
sidebar: Sidebar,
version: LoadedVersion,
):
function findFirstLink(sidebar: Sidebar):
| {
isAutoGeneratedIndex: false;
id: string;
label: string;
}
| {
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 {
path: version.docs.find(
(doc) => doc.id === item.id || doc.unversionedId === item.id,
)!.permalink,
isAutoGeneratedIndex: false,
id: item.id,
label: item.label ?? item.id,
};
} else if (item.type === 'category') {
if (item.link?.type === 'doc') {
const categoryLinkId = item.link.id;
return {
path: version.docs.find(
(doc) =>
doc.id === categoryLinkId ||
doc.unversionedId === categoryLinkId,
)!.permalink,
isAutoGeneratedIndex: false,
id: item.link.id,
label: item.label,
};
} else if (item.link?.type === 'generated-index') {
return {
path: normalizeUrl([version.versionPath, item.link.slug]),
isAutoGeneratedIndex: true,
slug: item.link.slug,
label: item.label,
};
} else {
const doc = findFirstLink(item.items, version);
const doc = findFirstLink(item.items);
if (doc) {
return doc;
}
@@ -330,7 +328,7 @@ Available document ids are:
getCategoryGeneratedIndexList,
getCategoryGeneratedIndexNavigation,
checkSidebarsDocIds,
findFirstLink: (id, version) => findFirstLink(sidebars[id], version),
findFirstLink: (id) => findFirstLink(sidebars[id]),
};
}