feat(v2): allow nested sidebar category shorthand syntax (#2444)

* feat(v2): allow sidebar category shorthand syntax also for nested elements

* Update sidebars-category-shorthand.js

* Update sidebars-category-shorthand.js

Co-authored-by: Yangshun Tay <tay.yang.shun@gmail.com>
This commit is contained in:
Sébastien Lorber 2020-03-25 11:59:51 -05:00 committed by GitHub
parent 85c124e3f1
commit 201c663318
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 116 additions and 52 deletions

View file

@ -15,8 +15,28 @@ import {
SidebarItemRaw,
SidebarItemLink,
SidebarItemDoc,
SidebarCategoryShorthandRaw,
} from './types';
function isCategoryShorthand(
item: SidebarItemRaw,
): item is SidebarCategoryShorthandRaw {
return typeof item !== 'string' && !item.type;
}
/**
* Convert {category1: [item1,item2]} shorthand syntax to long-form syntax
*/
function normalizeCategoryShorthand(
sidebar: SidebarCategoryShorthandRaw,
): SidebarItemCategoryRaw[] {
return Object.entries(sidebar).map(([label, items]) => ({
type: 'category',
label,
items,
}));
}
/**
* Check that item contains only allowed keys.
*/
@ -75,27 +95,29 @@ function assertIsLink(item: any): asserts item is SidebarItemLink {
* Normalizes recursively item and all its children. Ensures that at the end
* each item will be an object with the corresponding type.
*/
function normalizeItem(item: SidebarItemRaw): SidebarItem {
function normalizeItem(item: SidebarItemRaw): SidebarItem[] {
if (typeof item === 'string') {
return {
type: 'doc',
id: item,
};
return [
{
type: 'doc',
id: item,
},
];
}
if (!item.type) {
throw new Error(`Unknown sidebar item "${JSON.stringify(item)}".`);
if (isCategoryShorthand(item)) {
return normalizeCategoryShorthand(item).flatMap(normalizeItem);
}
switch (item.type) {
case 'category':
assertIsCategory(item);
return {...item, items: item.items.map(normalizeItem)};
return [{...item, items: item.items.flatMap(normalizeItem)}];
case 'link':
assertIsLink(item);
return item;
return [item];
case 'ref':
case 'doc':
assertIsDoc(item);
return item;
return [item];
default:
throw new Error(`Unknown sidebar item type: ${item.type}`);
}
@ -107,20 +129,11 @@ function normalizeItem(item: SidebarItemRaw): SidebarItem {
function normalizeSidebar(sidebars: SidebarRaw): Sidebar {
return Object.entries(sidebars).reduce(
(acc: Sidebar, [sidebarId, sidebar]) => {
let normalizedSidebar: SidebarItemRaw[];
const normalizedSidebar: SidebarItemRaw[] = Array.isArray(sidebar)
? sidebar
: normalizeCategoryShorthand(sidebar);
if (!Array.isArray(sidebar)) {
// Convert sidebar to a more generic structure.
normalizedSidebar = Object.entries(sidebar).map(([label, items]) => ({
type: 'category',
label,
items,
}));
} else {
normalizedSidebar = sidebar;
}
acc[sidebarId] = normalizedSidebar.map(normalizeItem);
acc[sidebarId] = normalizedSidebar.flatMap(normalizeItem);
return acc;
},