Skip to content

Commit

Permalink
feat(content-docs): allow omitting enclosing array consistently for c…
Browse files Browse the repository at this point in the history
…ategory shorthand (#6602)

* feat(content-docs): allow omitting enclosing array consistently for category shorthand

* update snapshot

* fix doc
  • Loading branch information
Josh-Cena authored Feb 4, 2022
1 parent e3fd3e7 commit 0c4dc00
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`normalization normalizes shorthands 1`] = `
Object {
"sidebar": Array [
Object {
"items": Array [
Object {
"id": "doc1",
"type": "doc",
},
Object {
"id": "doc2",
"type": "doc",
},
],
"label": "Category",
"type": "category",
},
Object {
"items": Array [
Object {
"items": Array [
Object {
"id": "doc3",
"type": "doc",
},
Object {
"id": "doc4",
"type": "doc",
},
],
"label": "Subcategory 1",
"type": "category",
},
Object {
"items": Array [
Object {
"id": "doc5",
"type": "doc",
},
Object {
"id": "doc6",
"type": "doc",
},
],
"label": "Subcategory 2",
"type": "category",
},
],
"label": "Category 2",
"type": "category",
},
],
}
`;

exports[`normalization normalizes shorthands 2`] = `
Object {
"sidebar": Array [
Object {
"href": "https://google.com",
"label": "Google",
"type": "link",
},
Object {
"items": Array [
Object {
"id": "doc1",
"type": "doc",
},
Object {
"id": "doc2",
"type": "doc",
},
],
"label": "Category 1",
"type": "category",
},
Object {
"items": Array [
Object {
"id": "doc3",
"type": "doc",
},
Object {
"id": "doc4",
"type": "doc",
},
],
"label": "Category 2",
"type": "category",
},
],
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('loadSidebars', () => {
await expect(() =>
loadSidebars(sidebarPath, params),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Invalid category {\\"type\\":\\"category\\",\\"label\\":\\"Category Label\\",\\"items\\":\\"doc1\\"}: items must be an array"`,
`"Invalid category {\\"type\\":\\"category\\",\\"label\\":\\"Category Label\\",\\"items\\":\\"doc1\\"}: items must be an array of sidebar items or a category shorthand"`,
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {normalizeSidebars} from '../normalization';

describe('normalization', () => {
test('normalizes shorthands', () => {
expect(
normalizeSidebars({
sidebar: {
Category: ['doc1', 'doc2'],
'Category 2': {
'Subcategory 1': ['doc3', 'doc4'],
'Subcategory 2': ['doc5', 'doc6'],
},
},
}),
).toMatchSnapshot();

expect(
normalizeSidebars({
sidebar: [
{
type: 'link',
label: 'Google',
href: 'https://google.com',
},
{
'Category 1': ['doc1', 'doc2'],
'Category 2': ['doc3', 'doc4'],
},
],
}),
).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,20 @@ export function normalizeItem(
);
}
if (item.type === 'category') {
if (typeof item.items !== 'undefined' && !Array.isArray(item.items)) {
if (typeof item.items !== 'undefined' && typeof item.items !== 'object') {
throw new Error(
`Invalid category ${JSON.stringify(item)}: items must be an array`,
`Invalid category ${JSON.stringify(
item,
)}: items must be an array of sidebar items or a category shorthand`,
);
}
const normalizedCategory: NormalizedSidebarItemCategory = {
...item,
items: item.items.flatMap((subItem) => normalizeItem(subItem)),
items: Array.isArray(item.items)
? item.items.flatMap((subItem) => normalizeItem(subItem))
: normalizeCategoriesShorthand(item.items).flatMap((subItem) =>
normalizeItem(subItem),
),
};
return [normalizedCategory];
}
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-content-docs/src/sidebars/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ export type SidebarItemCategoryLink =
// The user-given configuration in sidebars.js, before normalization
export type SidebarItemCategoryConfig = Expand<
Optional<SidebarItemCategoryBase, 'collapsed' | 'collapsible'> & {
items: SidebarItemConfig[];
items: SidebarCategoriesShorthand | SidebarItemConfig[];
link?: SidebarItemCategoryLinkConfig;
}
>;

export type SidebarCategoriesShorthand = {
[sidebarCategory: string]: SidebarItemConfig[];
[sidebarCategory: string]: SidebarCategoriesShorthand | SidebarItemConfig[];
};

export type SidebarItemConfig =
Expand Down
Loading

0 comments on commit 0c4dc00

Please sign in to comment.