mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-13 09:07:29 +02:00
fix(content-docs): restore functionality when a category only has index page (#7385)
* fix(content-docs): restore functionality when a category only has index page * use this internally
This commit is contained in:
parent
c3880cc342
commit
6e10a48059
13 changed files with 258 additions and 46 deletions
|
@ -15,12 +15,20 @@ import type {
|
|||
ProcessedSidebars,
|
||||
SidebarItemCategoryLink,
|
||||
} from './types';
|
||||
import {getDocIds} from '../docs';
|
||||
import _ from 'lodash';
|
||||
|
||||
type SidebarPostProcessorParams = SidebarProcessorParams & {
|
||||
draftIds: Set<string>;
|
||||
};
|
||||
|
||||
function normalizeCategoryLink(
|
||||
category: ProcessedSidebarItemCategory,
|
||||
params: SidebarProcessorParams,
|
||||
params: SidebarPostProcessorParams,
|
||||
): SidebarItemCategoryLink | undefined {
|
||||
if (category.link?.type === 'doc' && params.draftIds.has(category.link.id)) {
|
||||
return undefined;
|
||||
}
|
||||
if (category.link?.type === 'generated-index') {
|
||||
// Default slug logic can be improved
|
||||
const getDefaultSlug = () =>
|
||||
|
@ -38,37 +46,42 @@ function normalizeCategoryLink(
|
|||
|
||||
function postProcessSidebarItem(
|
||||
item: ProcessedSidebarItem,
|
||||
params: SidebarProcessorParams,
|
||||
): SidebarItem {
|
||||
params: SidebarPostProcessorParams,
|
||||
): SidebarItem | null {
|
||||
if (item.type === 'category') {
|
||||
// Fail-fast if there's actually no subitems, no because all subitems are
|
||||
// drafts. This is likely a configuration mistake.
|
||||
if (item.items.length === 0 && !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.`,
|
||||
);
|
||||
}
|
||||
const category = {
|
||||
...item,
|
||||
collapsed: item.collapsed ?? params.sidebarOptions.sidebarCollapsed,
|
||||
collapsible: item.collapsible ?? params.sidebarOptions.sidebarCollapsible,
|
||||
link: normalizeCategoryLink(item, params),
|
||||
items: item.items.map((subItem) =>
|
||||
postProcessSidebarItem(subItem, params),
|
||||
),
|
||||
items: item.items
|
||||
.map((subItem) => postProcessSidebarItem(subItem, params))
|
||||
.filter((v): v is SidebarItem => Boolean(v)),
|
||||
};
|
||||
// If the current category doesn't have subitems, we render a normal link
|
||||
// instead.
|
||||
if (category.items.length === 0) {
|
||||
if (!category.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.`,
|
||||
);
|
||||
// Doesn't make sense to render an empty generated index page, so we
|
||||
// filter the entire category out as well.
|
||||
if (
|
||||
!category.link ||
|
||||
category.link.type === 'generated-index' ||
|
||||
params.draftIds.has(category.link.id)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return category.link.type === 'doc'
|
||||
? {
|
||||
type: 'doc',
|
||||
label: category.label,
|
||||
id: category.link.id,
|
||||
}
|
||||
: {
|
||||
type: 'link',
|
||||
label: category.label,
|
||||
href: category.link.permalink,
|
||||
};
|
||||
return {
|
||||
type: 'doc',
|
||||
label: category.label,
|
||||
id: category.link.id,
|
||||
};
|
||||
}
|
||||
// A non-collapsible category can't be collapsed!
|
||||
if (category.collapsible === false) {
|
||||
|
@ -76,6 +89,12 @@ function postProcessSidebarItem(
|
|||
}
|
||||
return category;
|
||||
}
|
||||
if (
|
||||
(item.type === 'doc' || item.type === 'ref') &&
|
||||
params.draftIds.has(item.id)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
|
@ -83,7 +102,11 @@ export function postProcessSidebars(
|
|||
sidebars: ProcessedSidebars,
|
||||
params: SidebarProcessorParams,
|
||||
): Sidebars {
|
||||
const draftIds = new Set(params.drafts.flatMap(getDocIds));
|
||||
|
||||
return _.mapValues(sidebars, (sidebar) =>
|
||||
sidebar.map((item) => postProcessSidebarItem(item, params)),
|
||||
sidebar
|
||||
.map((item) => postProcessSidebarItem(item, {...params, draftIds}))
|
||||
.filter((v): v is SidebarItem => Boolean(v)),
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue