From 4e5a03fb88604ac853cedfa8cbd07c268bc8d8dc Mon Sep 17 00:00:00 2001 From: Endi Date: Thu, 14 Nov 2019 23:36:15 +0700 Subject: [PATCH] fix(v2): throw error if first level item of a sidebar is not category (#1994) --- .../sidebars/sidebars-first-level-not-category.js | 13 +++++++++++++ .../src/__tests__/sidebars.test.ts | 10 ++++++++++ .../docusaurus-plugin-content-docs/src/sidebars.ts | 7 +++++++ 3 files changed, 30 insertions(+) create mode 100644 packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/sidebars/sidebars-first-level-not-category.js diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/sidebars/sidebars-first-level-not-category.js b/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/sidebars/sidebars-first-level-not-category.js new file mode 100644 index 0000000000..17b0f9bb7d --- /dev/null +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/sidebars/sidebars-first-level-not-category.js @@ -0,0 +1,13 @@ +module.exports = { + docs: [ + { + type: 'category', + label: 'Getting Started', + items: ['greeting'], + }, + { + type: 'doc', + id: 'api', + }, + ], +}; diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/sidebars.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/sidebars.test.ts index e2f9b66a11..9260cade73 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/sidebars.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/sidebars.test.ts @@ -34,6 +34,16 @@ describe('loadSidebars', () => { ); }); + test('sidebars with first level not a category', async () => { + const sidebarPath = path.join( + fixtureDir, + 'sidebars-first-level-not-category', + ); + expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot( + `"Error loading {\\"type\\":\\"doc\\",\\"id\\":\\"api\\"}. First level item of a sidebar must be a category"`, + ); + }); + test('sidebars with unknown sidebar item type', async () => { const sidebarPath = path.join(fixtureDir, 'sidebars-unknown-type.json'); expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot( diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars.ts b/packages/docusaurus-plugin-content-docs/src/sidebars.ts index be972049a5..462e6b6de2 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars.ts @@ -39,6 +39,13 @@ function normalizeCategory( category: SidebarItemCategoryRaw, level = 0, ): SidebarItemCategory { + if (level === 0 && category.type !== 'category') { + throw new Error( + `Error loading ${JSON.stringify( + category, + )}. First level item of a sidebar must be a category`, + ); + } assertItem(category, ['items', 'label']); if (!Array.isArray(category.items)) {