Skip to content

Commit

Permalink
fix(v2): throw error if first level item of a sidebar is not category (
Browse files Browse the repository at this point in the history
  • Loading branch information
endiliey committed Nov 14, 2019
1 parent 472a1a6 commit 4e5a03f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
docs: [
{
type: 'category',
label: 'Getting Started',
items: ['greeting'],
},
{
type: 'doc',
id: 'api',
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions packages/docusaurus-plugin-content-docs/src/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 4e5a03f

Please sign in to comment.