-
Notifications
You must be signed in to change notification settings - Fork 2
/
sidebarItemsGenerator.ts
40 lines (37 loc) · 1.3 KB
/
sidebarItemsGenerator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type { NormalizedSidebarItem, SidebarItemConfig, SidebarItemsGeneratorArgs } from '@docusaurus/plugin-content-docs/lib/sidebars/types';
interface PostProcessArgs extends Omit<SidebarItemsGeneratorArgs, 'item'> {
item: NormalizedSidebarItem & {
customProps?: {
additionalItems?: Array<
SidebarItemConfig & {
position?: number;
}
>;
};
};
}
async function postProcess({ item, ...args }: PostProcessArgs) {
if (item.type === 'category') {
// Recurse through children
for (const childItem of item.items) {
await postProcess({ item: childItem, ...args });
}
// Add additional items
if (item.customProps?.additionalItems) {
for (const { position, ...additionalItem } of item.customProps.additionalItems) {
if (position !== undefined) {
item.items.splice(position - 1, 0, additionalItem);
} else {
item.items.push(additionalItem);
}
}
}
}
}
export default async function sidebarItemsGenerator({ defaultSidebarItemsGenerator, item, ...args }: SidebarItemsGeneratorArgs) {
const sidebarItems = await defaultSidebarItemsGenerator({ item, ...args });
for (const item of sidebarItems) {
await postProcess({ item, defaultSidebarItemsGenerator, ...args });
}
return sidebarItems;
}