mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-14 01:27:35 +02:00
fix(content-docs): improve sidebar shorthand normalization error message (#6745)
This commit is contained in:
parent
9562a5d203
commit
2d93750caf
4 changed files with 74 additions and 25 deletions
|
@ -58,7 +58,7 @@ describe('loadSidebars', () => {
|
||||||
await expect(() =>
|
await expect(() =>
|
||||||
loadSidebars(sidebarPath, params),
|
loadSidebars(sidebarPath, params),
|
||||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||||
`"Invalid category {\\"type\\":\\"category\\",\\"label\\":\\"Category Label\\",\\"items\\":\\"doc1\\"}: items must be an array of sidebar items or a category shorthand"`,
|
`"Invalid sidebar items collection [36m\`\\"doc1\\"\`[39m in [36m\`items\`[39m of the category [34m[1mCategory Label[22m[39m: it must either be an array of sidebar items or a shorthand notation (which doesn't contain a [36m\`type\`[39m property). See [36m[4mhttps://docusaurus.io/docs/sidebar/items[24m[39m for all valid syntaxes."`,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -37,4 +37,53 @@ describe('normalization', () => {
|
||||||
}),
|
}),
|
||||||
).toMatchSnapshot();
|
).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
test('rejects some invalid cases', () => {
|
||||||
|
expect(() =>
|
||||||
|
normalizeSidebars({
|
||||||
|
sidebar: {
|
||||||
|
Category: {type: 'autogenerated', dirName: 'foo'},
|
||||||
|
Category2: {type: 'autogenerated', dirName: 'bar'},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
).toThrowErrorMatchingInlineSnapshot(
|
||||||
|
`"Invalid sidebar items collection [36m\`{\\"type\\":\\"autogenerated\\",\\"dirName\\":\\"foo\\"}\`[39m in [36m\`items\`[39m of the category [34m[1mCategory[22m[39m: it must either be an array of sidebar items or a shorthand notation (which doesn't contain a [36m\`type\`[39m property). See [36m[4mhttps://docusaurus.io/docs/sidebar/items[24m[39m for all valid syntaxes."`,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(() =>
|
||||||
|
normalizeSidebars({
|
||||||
|
sidebar: [
|
||||||
|
'foo',
|
||||||
|
{
|
||||||
|
Category: {
|
||||||
|
type: 'category',
|
||||||
|
items: ['bar', 'baz'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
).toThrowErrorMatchingInlineSnapshot(
|
||||||
|
`"Invalid sidebar items collection [36m\`{\\"type\\":\\"category\\",\\"items\\":[\\"bar\\",\\"baz\\"]}\`[39m in [36m\`items\`[39m of the category [34m[1mCategory[22m[39m: it must either be an array of sidebar items or a shorthand notation (which doesn't contain a [36m\`type\`[39m property). See [36m[4mhttps://docusaurus.io/docs/sidebar/items[24m[39m for all valid syntaxes."`,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(() =>
|
||||||
|
normalizeSidebars({
|
||||||
|
sidebar: [
|
||||||
|
'foo',
|
||||||
|
{
|
||||||
|
Category: 'bar',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
).toThrowErrorMatchingInlineSnapshot(
|
||||||
|
`"Invalid sidebar items collection [36m\`\\"bar\\"\`[39m in [36m\`items\`[39m of the category [34m[1mCategory[22m[39m: it must either be an array of sidebar items or a shorthand notation (which doesn't contain a [36m\`type\`[39m property). See [36m[4mhttps://docusaurus.io/docs/sidebar/items[24m[39m for all valid syntaxes."`,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(() =>
|
||||||
|
normalizeSidebars({
|
||||||
|
sidebar: 'item',
|
||||||
|
}),
|
||||||
|
).toThrowErrorMatchingInlineSnapshot(
|
||||||
|
`"Invalid sidebar items collection [36m\`\\"item\\"\`[39m in sidebar [34m[1msidebar[22m[39m: it must either be an array of sidebar items or a shorthand notation (which doesn't contain a [36m\`type\`[39m property). See [36m[4mhttps://docusaurus.io/docs/sidebar/items[24m[39m for all valid syntaxes."`,
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -18,6 +18,7 @@ import type {
|
||||||
} from './types';
|
} from './types';
|
||||||
import {isCategoriesShorthand} from './utils';
|
import {isCategoriesShorthand} from './utils';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
import logger from '@docusaurus/logger';
|
||||||
|
|
||||||
function normalizeCategoriesShorthand(
|
function normalizeCategoriesShorthand(
|
||||||
sidebar: SidebarCategoriesShorthand,
|
sidebar: SidebarCategoriesShorthand,
|
||||||
|
@ -37,32 +38,18 @@ export function normalizeItem(
|
||||||
item: SidebarItemConfig,
|
item: SidebarItemConfig,
|
||||||
): NormalizedSidebarItem[] {
|
): NormalizedSidebarItem[] {
|
||||||
if (typeof item === 'string') {
|
if (typeof item === 'string') {
|
||||||
return [
|
return [{type: 'doc', id: item}];
|
||||||
{
|
|
||||||
type: 'doc',
|
|
||||||
id: item,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
if (isCategoriesShorthand(item)) {
|
if (isCategoriesShorthand(item)) {
|
||||||
return normalizeCategoriesShorthand(item).flatMap((subItem) =>
|
// This will never throw anyways
|
||||||
normalizeItem(subItem),
|
return normalizeSidebar(item, 'sidebar items slice');
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (item.type === 'category') {
|
if (item.type === 'category') {
|
||||||
if (typeof item.items !== 'undefined' && typeof item.items !== 'object') {
|
|
||||||
throw new Error(
|
|
||||||
`Invalid category ${JSON.stringify(
|
|
||||||
item,
|
|
||||||
)}: items must be an array of sidebar items or a category shorthand`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const normalizedCategory: NormalizedSidebarItemCategory = {
|
const normalizedCategory: NormalizedSidebarItemCategory = {
|
||||||
...item,
|
...item,
|
||||||
items: Array.isArray(item.items)
|
items: normalizeSidebar(
|
||||||
? item.items.flatMap((subItem) => normalizeItem(subItem))
|
item.items,
|
||||||
: normalizeCategoriesShorthand(item.items).flatMap((subItem) =>
|
logger.interpolate`code=${'items'} of the category name=${item.label}`,
|
||||||
normalizeItem(subItem),
|
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
return [normalizedCategory];
|
return [normalizedCategory];
|
||||||
|
@ -70,7 +57,18 @@ export function normalizeItem(
|
||||||
return [item];
|
return [item];
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeSidebar(sidebar: SidebarConfig): NormalizedSidebar {
|
function normalizeSidebar(
|
||||||
|
sidebar: SidebarConfig,
|
||||||
|
place: string,
|
||||||
|
): NormalizedSidebar {
|
||||||
|
if (!Array.isArray(sidebar) && !isCategoriesShorthand(sidebar)) {
|
||||||
|
throw new Error(
|
||||||
|
logger.interpolate`Invalid sidebar items collection code=${JSON.stringify(
|
||||||
|
sidebar,
|
||||||
|
)} in ${place}: it must either be an array of sidebar items or a shorthand notation (which doesn't contain a code=${'type'} property). See path=${'https://docusaurus.io/docs/sidebar/items'} for all valid syntaxes.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const normalizedSidebar = Array.isArray(sidebar)
|
const normalizedSidebar = Array.isArray(sidebar)
|
||||||
? sidebar
|
? sidebar
|
||||||
: normalizeCategoriesShorthand(sidebar);
|
: normalizeCategoriesShorthand(sidebar);
|
||||||
|
@ -81,5 +79,7 @@ function normalizeSidebar(sidebar: SidebarConfig): NormalizedSidebar {
|
||||||
export function normalizeSidebars(
|
export function normalizeSidebars(
|
||||||
sidebars: SidebarsConfig,
|
sidebars: SidebarsConfig,
|
||||||
): NormalizedSidebars {
|
): NormalizedSidebars {
|
||||||
return _.mapValues(sidebars, normalizeSidebar);
|
return _.mapValues(sidebars, (sidebar, id) =>
|
||||||
|
normalizeSidebar(sidebar, logger.interpolate`sidebar name=${id}`),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ import type {DocMetadataBase, DocNavLink} from '../types';
|
||||||
export function isCategoriesShorthand(
|
export function isCategoriesShorthand(
|
||||||
item: SidebarItemConfig,
|
item: SidebarItemConfig,
|
||||||
): item is SidebarCategoriesShorthand {
|
): item is SidebarCategoriesShorthand {
|
||||||
return typeof item !== 'string' && !item.type;
|
return typeof item === 'object' && !item.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function transformSidebarItems(
|
export function transformSidebarItems(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue