forked from nodejs/doc-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidebar.mjs
More file actions
59 lines (49 loc) · 1.86 KB
/
sidebar.mjs
File metadata and controls
59 lines (49 loc) · 1.86 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { SIDEBAR_GROUPS } from '../../constants.mjs';
/**
* @deprecated This is being exported temporarily during the transition period.
* Reverse lookup: filename (e.g. 'fs.html') -> groupName, used as category
* fallback for pages without explicit category in metadata.
*/
export const fileToGroup = new Map(
SIDEBAR_GROUPS.flatMap(({ groupName, items }) =>
items.map(item => [item, groupName])
)
);
/**
* Builds grouped sidebar navigation from categorized page entries.
* Pages without a category are placed under the provided default group.
*
* @param {Array<{ label: string, link: string, category?: string }>} items
* @param {string} [defaultGroupName='Others']
* @returns {Array<{ groupName: string, items: Array<{ label: string, link: string }> }>}
*/
export const buildSideBarGroups = (items, defaultGroupName = 'Others') => {
const groups = new Map();
const others = [];
// Group entries by category while preserving insertion order
for (const { label, link, category } of items) {
const linkFilename = link.split('/').at(-1);
// Skip index pages as they are typically the main entry point for a section
// and may not need to be listed separately in the sidebar.
if (linkFilename === 'index.html') {
continue;
}
const resolvedCategory = category ?? fileToGroup.get(linkFilename);
if (!resolvedCategory) {
others.push({ label, link });
continue;
}
const items = groups.get(resolvedCategory) ?? [];
items.push({ label, link });
groups.set(resolvedCategory, items);
}
// Convert the groups map to an array while preserving the original order of categories
const orderedGroups = [...groups.entries()].map(([groupName, items]) => ({
groupName,
items,
}));
if (others.length > 0) {
orderedGroups.push({ groupName: defaultGroupName, items: others });
}
return orderedGroups;
};