mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-14 17:47:40 +02:00
fix(content-docs): render category with no subitems as a normal link (#6495)
This commit is contained in:
parent
049b2c84c6
commit
3573b5e4a9
6 changed files with 120 additions and 5 deletions
|
@ -209,6 +209,12 @@ describe('processSidebars', () => {
|
|||
// typing error needing refactor
|
||||
permalink: undefined,
|
||||
},
|
||||
items: [
|
||||
{
|
||||
type: 'doc',
|
||||
id: 'foo',
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
|
@ -232,11 +238,73 @@ describe('processSidebars', () => {
|
|||
slug: 'generated-cat-index-slug',
|
||||
permalink: '/docs/1.0.0/generated-cat-index-slug',
|
||||
},
|
||||
items: [],
|
||||
items: [
|
||||
{
|
||||
type: 'doc',
|
||||
id: 'foo',
|
||||
},
|
||||
],
|
||||
collapsible: true,
|
||||
collapsed: true,
|
||||
},
|
||||
],
|
||||
} as Sidebars);
|
||||
});
|
||||
|
||||
test('transforms category without subitems', async () => {
|
||||
const sidebarSlice: SidebarItem[] = [
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category',
|
||||
link: {
|
||||
type: 'generated-index',
|
||||
permalink: 'generated/permalink',
|
||||
},
|
||||
items: [],
|
||||
},
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category 2',
|
||||
link: {
|
||||
type: 'doc',
|
||||
id: 'doc ID',
|
||||
},
|
||||
items: [],
|
||||
},
|
||||
];
|
||||
|
||||
const processedSidebar = await testProcessSidebars(
|
||||
{sidebar: sidebarSlice},
|
||||
{},
|
||||
);
|
||||
|
||||
expect(processedSidebar).toEqual({
|
||||
sidebar: [
|
||||
{
|
||||
type: 'link',
|
||||
label: 'Category',
|
||||
href: 'generated/permalink',
|
||||
},
|
||||
{
|
||||
type: 'doc',
|
||||
label: 'Category 2',
|
||||
id: 'doc ID',
|
||||
},
|
||||
],
|
||||
} as Sidebars);
|
||||
|
||||
await expect(async () => {
|
||||
await testProcessSidebars({
|
||||
sidebar: [
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Bad category',
|
||||
items: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
}).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"Sidebar category Bad category has neither any subitem nor a link. This makes this item not able to link to anything."`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -117,6 +117,34 @@ async function processSidebar(
|
|||
item: NormalizedSidebarItem,
|
||||
): Promise<SidebarItem[]> {
|
||||
if (item.type === 'category') {
|
||||
// If the current category doesn't have subitems, we render a normal doc link instead.
|
||||
if (item.items.length === 0) {
|
||||
if (!item.link) {
|
||||
throw new Error(
|
||||
`Sidebar category ${item.label} has neither any subitem nor a link. This makes this item not able to link to anything.`,
|
||||
);
|
||||
}
|
||||
switch (item.link.type) {
|
||||
case 'doc':
|
||||
return [
|
||||
{
|
||||
type: 'doc',
|
||||
label: item.label,
|
||||
id: item.link.id,
|
||||
},
|
||||
];
|
||||
case 'generated-index':
|
||||
return [
|
||||
{
|
||||
type: 'link',
|
||||
label: item.label,
|
||||
href: item.link.permalink,
|
||||
},
|
||||
];
|
||||
default:
|
||||
throw new Error('Unexpected sidebar category link type');
|
||||
}
|
||||
}
|
||||
return [await processCategoryItem(item)];
|
||||
}
|
||||
if (item.type === 'autogenerated') {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue