feat(content-docs): sidebar category linking to document or auto-generated index page (#5830)

Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
Co-authored-by: Armano <armano2@users.noreply.github.com>
Co-authored-by: Alexey Pyltsyn <lex61rus@gmail.com>
This commit is contained in:
Sébastien Lorber 2021-12-03 14:44:59 +01:00 committed by GitHub
parent 95f911efef
commit cfae5d0933
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
105 changed files with 3904 additions and 816 deletions

View file

@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import type {SidebarOptions} from '../types';
import type {NormalizeSidebarsParams, SidebarOptions} from '../types';
import type {
NormalizedSidebarItem,
NormalizedSidebar,
@ -15,9 +15,31 @@ import type {
SidebarItemConfig,
SidebarConfig,
SidebarsConfig,
SidebarItemCategoryLink,
NormalizedSidebarItemCategory,
} from './types';
import {mapValues} from 'lodash';
import {isCategoriesShorthand} from './utils';
import {mapValues} from 'lodash';
import {normalizeUrl} from '@docusaurus/utils';
function normalizeCategoryLink(
category: SidebarItemCategoryConfig,
params: NormalizeSidebarsParams,
): SidebarItemCategoryLink | undefined {
if (category.link?.type === 'generated-index') {
// default slug logic can be improved
const getDefaultSlug = () =>
`/category/${params.categoryLabelSlugger.slug(category.label)}`;
const slug = category.link.slug ?? getDefaultSlug();
const permalink = normalizeUrl([params.version.versionPath, slug]);
return {
...category.link,
slug,
permalink,
};
}
return category.link;
}
function normalizeCategoriesShorthand(
sidebar: SidebarCategoriesShorthand,
@ -36,9 +58,9 @@ function normalizeCategoriesShorthand(
* Normalizes recursively item and all its children. Ensures that at the end
* each item will be an object with the corresponding type.
*/
function normalizeItem(
export function normalizeItem(
item: SidebarItemConfig,
options: SidebarOptions,
options: NormalizeSidebarsParams,
): NormalizedSidebarItem[] {
if (typeof item === 'string') {
return [
@ -49,40 +71,42 @@ function normalizeItem(
];
}
if (isCategoriesShorthand(item)) {
return normalizeCategoriesShorthand(item, options).flatMap((subitem) =>
normalizeItem(subitem, options),
return normalizeCategoriesShorthand(item, options).flatMap((subItem) =>
normalizeItem(subItem, options),
);
}
return item.type === 'category'
? [
{
...item,
items: item.items.flatMap((subItem) =>
normalizeItem(subItem, options),
),
collapsible: item.collapsible ?? options.sidebarCollapsible,
collapsed: item.collapsed ?? options.sidebarCollapsed,
},
]
: [item];
if (item.type === 'category') {
const link = normalizeCategoryLink(item, options);
const normalizedCategory: NormalizedSidebarItemCategory = {
...item,
link,
items: (item.items ?? []).flatMap((subItem) =>
normalizeItem(subItem, options),
),
collapsible: item.collapsible ?? options.sidebarCollapsible,
collapsed: item.collapsed ?? options.sidebarCollapsed,
};
return [normalizedCategory];
}
return [item];
}
function normalizeSidebar(
sidebar: SidebarConfig,
options: SidebarOptions,
options: NormalizeSidebarsParams,
): NormalizedSidebar {
const normalizedSidebar = Array.isArray(sidebar)
? sidebar
: normalizeCategoriesShorthand(sidebar, options);
return normalizedSidebar.flatMap((subitem) =>
normalizeItem(subitem, options),
return normalizedSidebar.flatMap((subItem) =>
normalizeItem(subItem, options),
);
}
export function normalizeSidebars(
sidebars: SidebarsConfig,
options: SidebarOptions,
params: NormalizeSidebarsParams,
): NormalizedSidebars {
return mapValues(sidebars, (subitem) => normalizeSidebar(subitem, options));
return mapValues(sidebars, (items) => normalizeSidebar(items, params));
}