feat(v2): add custom props for consumption by swizzled sidebar (#3888)

This commit is contained in:
Oliver Ullman 2020-12-10 11:49:21 -07:00 committed by GitHub
parent 0b05806593
commit b11c24b752
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 11 deletions

View file

@ -361,6 +361,7 @@ describe('transformSidebarItems', () => {
collapsed: false,
label: 'Subcategory 1',
items: [{type: 'doc', id: 'doc1'}],
customProps: {fakeProp: false},
},
{
type: 'category',
@ -372,7 +373,9 @@ describe('transformSidebarItems', () => {
type: 'category',
collapsed: false,
label: 'Sub sub category 1',
items: [{type: 'doc', id: 'doc3'}],
items: [
{type: 'doc', id: 'doc3', customProps: {lorem: 'ipsum'}},
],
},
],
},
@ -407,6 +410,7 @@ describe('transformSidebarItems', () => {
collapsed: false,
label: 'MODIFIED LABEL: Subcategory 1',
items: [{type: 'doc', id: 'doc1'}],
customProps: {fakeProp: false},
},
{
type: 'category',
@ -418,7 +422,9 @@ describe('transformSidebarItems', () => {
type: 'category',
collapsed: false,
label: 'MODIFIED LABEL: Sub sub category 1',
items: [{type: 'doc', id: 'doc3'}],
items: [
{type: 'doc', id: 'doc3', customProps: {lorem: 'ipsum'}},
],
},
],
},

View file

@ -21,13 +21,17 @@ declare module '@docusaurus/plugin-content-docs-types' {
permalinkToSidebar: PermalinkToSidebar;
};
export type PropSidebarItemLink = {
type PropsSidebarItemBase = {
customProps?: object;
};
export type PropSidebarItemLink = PropsSidebarItemBase & {
type: 'link';
href: string;
label: string;
};
export type PropSidebarItemCategory = {
export type PropSidebarItemCategory = PropsSidebarItemBase & {
type: 'category';
label: string;
items: PropSidebarItem[];

View file

@ -39,6 +39,7 @@ Available document ids=
type: 'link',
label: sidebar_label || title,
href: permalink,
customProps: item.customProps,
};
};

View file

@ -11,6 +11,7 @@ import importFresh from 'import-fresh';
import {
Sidebars,
SidebarItem,
SidebarItemBase,
SidebarItemLink,
SidebarItemDoc,
Sidebar,
@ -20,7 +21,7 @@ import {
import {mapValues, flatten, difference} from 'lodash';
import {getElementsAround} from '@docusaurus/utils';
type SidebarItemCategoryJSON = {
type SidebarItemCategoryJSON = SidebarItemBase & {
type: 'category';
label: string;
items: SidebarItemJSON[];
@ -96,7 +97,7 @@ function assertItem<K extends string>(
function assertIsCategory(
item: unknown,
): asserts item is SidebarItemCategoryJSON {
assertItem(item, ['items', 'label', 'collapsed']);
assertItem(item, ['items', 'label', 'collapsed', 'customProps']);
if (typeof item.label !== 'string') {
throw new Error(
`Error loading ${JSON.stringify(item)}. "label" must be a string.`,
@ -116,7 +117,7 @@ function assertIsCategory(
}
function assertIsDoc(item: unknown): asserts item is SidebarItemDoc {
assertItem(item, ['id']);
assertItem(item, ['id', 'customProps']);
if (typeof item.id !== 'string') {
throw new Error(
`Error loading ${JSON.stringify(item)}. "id" must be a string.`,
@ -125,7 +126,7 @@ function assertIsDoc(item: unknown): asserts item is SidebarItemDoc {
}
function assertIsLink(item: unknown): asserts item is SidebarItemLink {
assertItem(item, ['href', 'label']);
assertItem(item, ['href', 'label', 'customProps']);
if (typeof item.href !== 'string') {
throw new Error(
`Error loading ${JSON.stringify(item)}. "href" must be a string.`,

View file

@ -75,18 +75,22 @@ export type PluginOptions = MetadataOptions &
includeCurrentVersion: boolean;
};
export type SidebarItemDoc = {
export type SidebarItemBase = {
customProps?: object;
};
export type SidebarItemDoc = SidebarItemBase & {
type: 'doc' | 'ref';
id: string;
};
export type SidebarItemLink = {
export type SidebarItemLink = SidebarItemBase & {
type: 'link';
href: string;
label: string;
};
export type SidebarItemCategory = {
export type SidebarItemCategory = SidebarItemBase & {
type: 'category';
label: string;
items: SidebarItem[];