diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/validation.test.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/validation.test.ts index 49ac30fdfe..28a0d0eb19 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/validation.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/validation.test.ts @@ -47,6 +47,42 @@ describe('validateSidebars', () => { }); }); +describe('html item type', () => { + test('requires a value', () => { + const sidebars: SidebarsConfig = { + sidebar1: [ + { + // @ts-expect-error - test missing value + type: 'html', + }, + ], + }; + expect(() => validateSidebars(sidebars)) + .toThrowErrorMatchingInlineSnapshot(` + "{ + \\"type\\": \\"html\\", + [41m\\"value\\"[0m[31m [1]: -- missing --[0m + } + [31m + [1] \\"value\\" is required[0m" + `); + }); + + test('accepts valid values', () => { + const sidebars: SidebarsConfig = { + sidebar1: [ + { + type: 'html', + value: '
Hello, World!
', + defaultStyle: true, + className: 'foo', + }, + ], + }; + validateSidebars(sidebars); + }); +}); + describe('validateCategoryMetadataFile', () => { // TODO add more tests diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts index 331887d122..e141e55464 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts @@ -27,6 +27,12 @@ export type SidebarItemDoc = SidebarItemBase & { id: string; }; +export type SidebarItemHtml = SidebarItemBase & { + type: 'html'; + value: string; + defaultStyle?: boolean; +}; + export type SidebarItemLink = SidebarItemBase & { type: 'link'; href: string; @@ -87,6 +93,7 @@ export type SidebarCategoriesShorthand = { export type SidebarItemConfig = | SidebarItemDoc + | SidebarItemHtml | SidebarItemLink | SidebarItemAutogenerated | SidebarItemCategoryConfig @@ -108,6 +115,7 @@ export type NormalizedSidebarItemCategory = Expand< export type NormalizedSidebarItem = | SidebarItemDoc + | SidebarItemHtml | SidebarItemLink | NormalizedSidebarItemCategory | SidebarItemAutogenerated; @@ -131,6 +139,7 @@ export type SidebarItemCategoryWithGeneratedIndex = export type SidebarItem = | SidebarItemDoc + | SidebarItemHtml | SidebarItemLink | SidebarItemCategory; @@ -158,7 +167,12 @@ export type PropSidebarItemLink = SidebarItemLink & { docId?: string; }; -export type PropSidebarItem = PropSidebarItemLink | PropSidebarItemCategory; +export type PropSidebarItemHtml = SidebarItemHtml; + +export type PropSidebarItem = + | PropSidebarItemLink + | PropSidebarItemCategory + | PropSidebarItemHtml; export type PropSidebar = PropSidebarItem[]; export type PropSidebars = { [sidebarId: string]: PropSidebar; diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/validation.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/validation.ts index ac00bf455c..7eb814172e 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/validation.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/validation.ts @@ -12,6 +12,7 @@ import type { SidebarItemBase, SidebarItemAutogenerated, SidebarItemDoc, + SidebarItemHtml, SidebarItemLink, SidebarItemCategoryConfig, SidebarItemCategoryLink, @@ -48,6 +49,12 @@ const sidebarItemDocSchema = sidebarItemBaseSchema.appendtest1
'}, testCategory({ href: undefined, items: [ + {type: 'html', value: 'test2
'}, testCategory({ href: '/itemPath', }), diff --git a/packages/docusaurus-theme-common/src/utils/docsUtils.tsx b/packages/docusaurus-theme-common/src/utils/docsUtils.tsx index 676ccee827..2ba9be962f 100644 --- a/packages/docusaurus-theme-common/src/utils/docsUtils.tsx +++ b/packages/docusaurus-theme-common/src/utils/docsUtils.tsx @@ -123,12 +123,13 @@ export function findFirstCategoryLink( for (const subItem of item.items) { if (subItem.type === 'link') { return subItem.href; - } - if (subItem.type === 'category') { + } else if (subItem.type === 'category') { const categoryLink = findFirstCategoryLink(subItem); if (categoryLink) { return categoryLink; } + } else if (subItem.type === 'html') { + // skip } else { throw new Error( `Unexpected category item type for ${JSON.stringify(subItem)}`, diff --git a/website/_dogfooding/docs-tests-sidebars.js b/website/_dogfooding/docs-tests-sidebars.js index 8be3194694..ec54a32d02 100644 --- a/website/_dogfooding/docs-tests-sidebars.js +++ b/website/_dogfooding/docs-tests-sidebars.js @@ -71,6 +71,33 @@ const sidebars = { }, ], }, + { + type: 'category', + label: 'HTML items tests', + items: [ + // title + { + type: 'html', + value: 'Some Text', + defaultStyle: true, + }, + // Divider + { + type: 'html', + value: + '', + }, + // Image + { + type: 'html', + defaultStyle: true, + value: ` + Powered by +