mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 07:37:19 +02:00
fix(v2): throw error if first level item of a sidebar is not category (#1994)
This commit is contained in:
parent
472a1a660c
commit
4e5a03fb88
3 changed files with 30 additions and 0 deletions
|
@ -0,0 +1,13 @@
|
||||||
|
module.exports = {
|
||||||
|
docs: [
|
||||||
|
{
|
||||||
|
type: 'category',
|
||||||
|
label: 'Getting Started',
|
||||||
|
items: ['greeting'],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'doc',
|
||||||
|
id: 'api',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
|
@ -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(
|
||||||
|
|
|
@ -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)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue