fix(v2): throw error if first level item of a sidebar is not category (#1994)

This commit is contained in:
Endi 2019-11-14 23:36:15 +07:00 committed by GitHub
parent 472a1a660c
commit 4e5a03fb88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 0 deletions

View file

@ -0,0 +1,13 @@
module.exports = {
docs: [
{
type: 'category',
label: 'Getting Started',
items: ['greeting'],
},
{
type: 'doc',
id: 'api',
},
],
};

View file

@ -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 () => { test('sidebars with unknown sidebar item type', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-unknown-type.json'); const sidebarPath = path.join(fixtureDir, 'sidebars-unknown-type.json');
expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot( expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(

View file

@ -39,6 +39,13 @@ function normalizeCategory(
category: SidebarItemCategoryRaw, category: SidebarItemCategoryRaw,
level = 0, level = 0,
): SidebarItemCategory { ): 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']); assertItem(category, ['items', 'label']);
if (!Array.isArray(category.items)) { if (!Array.isArray(category.items)) {