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
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@ Object {
"mainDocId": "hello",
"name": "current",
"path": "/docs",
"sidebars": Object {
"docs": Object {
"link": Object {
"label": "foo/bar",
"path": "/docs/foo/bar",
},
},
},
},
],
},
Expand Down Expand Up @@ -1007,6 +1015,14 @@ Object {
"mainDocId": "hello",
"name": "current",
"path": "/docs",
"sidebars": Object {
"docs": Object {
"link": Object {
"label": "foo/bar",
"path": "/docs/foo/bar",
},
},
},
},
],
},
Expand Down Expand Up @@ -2359,6 +2375,14 @@ Object {
"mainDocId": "team",
"name": "current",
"path": "/community/next",
"sidebars": Object {
"community": Object {
"link": Object {
"label": "team",
"path": "/community/next/team",
},
},
},
},
Object {
"docs": Array [
Expand All @@ -2373,6 +2397,14 @@ Object {
"mainDocId": "team",
"name": "1.0.0",
"path": "/community",
"sidebars": Object {
"version-1.0.0/community": Object {
"link": Object {
"label": "version-1.0.0/team",
"path": "/community/team",
},
},
},
},
],
},
Expand Down Expand Up @@ -3407,6 +3439,14 @@ Object {
"mainDocId": "hello",
"name": "current",
"path": "/docs/next",
"sidebars": Object {
"docs": Object {
"link": Object {
"label": "foo/bar",
"path": "/docs/next/foo/barSlug",
},
},
},
},
Object {
"docs": Array [
Expand All @@ -3426,6 +3466,14 @@ Object {
"mainDocId": "hello",
"name": "1.0.1",
"path": "/docs",
"sidebars": Object {
"VersionedSideBarNameDoesNotMatter/docs": Object {
"link": Object {
"label": "foo/bar",
"path": "/docs/foo/bar",
},
},
},
},
Object {
"docs": Array [
Expand All @@ -3450,6 +3498,14 @@ Object {
"mainDocId": "hello",
"name": "1.0.0",
"path": "/docs/1.0.0",
"sidebars": Object {
"version-1.0.0/docs": Object {
"link": Object {
"label": "version-1.0.0/foo/bar",
"path": "/docs/1.0.0/foo/barSlug",
},
},
},
},
Object {
"docs": Array [
Expand Down Expand Up @@ -3499,6 +3555,14 @@ Object {
"mainDocId": "rootAbsoluteSlug",
"name": "withSlugs",
"path": "/docs/withSlugs",
"sidebars": Object {
"version-1.0.1/docs": Object {
"link": Object {
"label": "version-withSlugs/rootAbsoluteSlug",
"path": "/docs/withSlugs/rootAbsoluteSlug",
},
},
},
},
],
},
Expand Down
31 changes: 31 additions & 0 deletions packages/docusaurus-plugin-content-docs/src/globalData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
* LICENSE file in the root directory of this source tree.
*/

import {mapValues} from 'lodash';
import {normalizeUrl} from '@docusaurus/utils';
import type {Sidebars} from './sidebars/types';
import {createSidebarsUtils} from './sidebars/utils';
import type {
DocMetadata,
GlobalDoc,
LoadedVersion,
GlobalVersion,
GlobalSidebar,
} from './types';

export function toGlobalDataDoc(doc: DocMetadata): GlobalDoc {
Expand All @@ -20,6 +25,31 @@ export function toGlobalDataDoc(doc: DocMetadata): GlobalDoc {
};
}

export function toGlobalSidebars(
sidebars: Sidebars,
version: LoadedVersion,
): Record<string, GlobalSidebar> {
const {getFirstLink} = createSidebarsUtils(sidebars);
return mapValues(sidebars, (sidebar, sidebarId) => {
const firstLink = getFirstLink(sidebarId);
if (!firstLink) {
return {};
}
return {
link: {
path:
firstLink.type === 'generated-index'
? normalizeUrl([version.versionPath, firstLink.slug])
: version.docs.find(
(doc) =>
doc.id === firstLink.id || doc.unversionedId === firstLink.id,
)!.permalink,
label: firstLink.label,
},
};
});
}

export function toGlobalDataVersion(version: LoadedVersion): GlobalVersion {
return {
name: version.versionName,
Expand All @@ -28,5 +58,6 @@ export function toGlobalDataVersion(version: LoadedVersion): GlobalVersion {
path: version.versionPath,
mainDocId: version.mainDocId,
docs: version.docs.map(toGlobalDataDoc),
sidebars: toGlobalSidebars(version.sidebars, version),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ declare module '@docusaurus/plugin-content-docs' {
export type VersionBanner = import('./types').VersionBanner;
type GlobalDataVersion = import('./types').GlobalVersion;
type GlobalDataDoc = import('./types').GlobalDoc;
type GlobalDataSidebar = import('./types').GlobalSidebar;
type VersionTag = import('./types').VersionTag;

export type {GlobalDataVersion, GlobalDataDoc};
export type {GlobalDataVersion, GlobalDataDoc, GlobalDataSidebar};

export type PropNavigationLink = {
readonly title: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('createSidebarsUtils', () => {
collapsible: true,
label: 'S2 Category',
items: [
{type: 'doc', id: 'doc3'},
{type: 'doc', id: 'doc3', label: 'Doc 3'},
{type: 'doc', id: 'doc4'},
],
},
Expand Down Expand Up @@ -95,14 +95,33 @@ describe('createSidebarsUtils', () => {
},
];

const sidebars: Sidebars = {sidebar1, sidebar2, sidebar3};
const sidebar4: Sidebar = [
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'S4 Category',
link: {
type: 'generated-index',
slug: '/s4-category-slug',
permalink: '/s4-category-permalink',
},
items: [
{type: 'doc', id: 'doc8'},
{type: 'doc', id: 'doc9'},
],
},
];

const sidebars: Sidebars = {sidebar1, sidebar2, sidebar3, sidebar4};

const {
getFirstDocIdOfFirstSidebar,
getSidebarNameByDocId,
getDocNavigation,
getCategoryGeneratedIndexNavigation,
getCategoryGeneratedIndexList,
getFirstLink,
} = createSidebarsUtils(sidebars);

test('getSidebarNameByDocId', async () => {
Expand All @@ -121,15 +140,15 @@ describe('createSidebarsUtils', () => {
});

test('getDocNavigation', async () => {
expect(getDocNavigation('doc1')).toEqual({
expect(getDocNavigation('doc1', 'doc1')).toEqual({
sidebarName: 'sidebar1',
previous: undefined,
next: {
type: 'doc',
id: 'doc2',
},
} as SidebarNavigation);
expect(getDocNavigation('doc2')).toEqual({
expect(getDocNavigation('doc2', 'doc2')).toEqual({
sidebarName: 'sidebar1',
previous: {
type: 'doc',
Expand All @@ -138,32 +157,33 @@ describe('createSidebarsUtils', () => {
next: undefined,
} as SidebarNavigation);

expect(getDocNavigation('doc3')).toEqual({
expect(getDocNavigation('doc3', 'doc3')).toEqual({
sidebarName: 'sidebar2',
previous: undefined,
next: {
type: 'doc',
id: 'doc4',
},
} as SidebarNavigation);
expect(getDocNavigation('doc4')).toEqual({
expect(getDocNavigation('doc4', 'doc4')).toEqual({
sidebarName: 'sidebar2',
previous: {
type: 'doc',
id: 'doc3',
label: 'Doc 3',
},
next: undefined,
} as SidebarNavigation);

expect(getDocNavigation('doc5')).toMatchObject({
expect(getDocNavigation('doc5', 'doc5')).toMatchObject({
sidebarName: 'sidebar3',
previous: undefined,
next: {
type: 'category',
label: 'S3 SubCategory',
},
} as SidebarNavigation);
expect(getDocNavigation('doc6')).toMatchObject({
expect(getDocNavigation('doc6', 'doc6')).toMatchObject({
sidebarName: 'sidebar3',
previous: {
type: 'category',
Expand All @@ -174,7 +194,7 @@ describe('createSidebarsUtils', () => {
id: 'doc7',
},
} as SidebarNavigation);
expect(getDocNavigation('doc7')).toMatchObject({
expect(getDocNavigation('doc7', 'doc7')).toMatchObject({
sidebarName: 'sidebar3',
previous: {
type: 'doc',
Expand Down Expand Up @@ -224,8 +244,35 @@ describe('createSidebarsUtils', () => {
type: 'category',
label: 'S3 SubSubCategory',
},
{
type: 'category',
label: 'S4 Category',
},
]);
});

test('getFirstLink', () => {
expect(getFirstLink('sidebar1')).toEqual({
id: 'doc1',
type: 'doc',
label: 'doc1',
});
expect(getFirstLink('sidebar2')).toEqual({
id: 'doc3',
type: 'doc',
label: 'Doc 3',
});
expect(getFirstLink('sidebar3')).toEqual({
id: 'doc5',
type: 'doc',
label: 'S3 Category',
});
expect(getFirstLink('sidebar4')).toEqual({
type: 'generated-index',
slug: '/s4-category-slug',
label: 'S4 Category',
});
});
});

describe('collectSidebarDocItems', () => {
Expand Down
Loading