forked from Panfactum/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeModuleList.ts
More file actions
75 lines (68 loc) · 1.89 KB
/
makeModuleList.ts
File metadata and controls
75 lines (68 loc) · 1.89 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Enhanced module type with optional sub-navigation
interface EnhancedModule {
type: string;
group: string;
module: string;
sub?: Array<{
path: string;
text: string;
sub?: Array<{ path: string; text: string }>;
}>;
hasContent?: boolean;
}
export function makeModuleDir(
modules: Array<{ type: string; group: string; module: string }>,
group: string,
type: string,
) {
return modules
.filter((module) => module.group === group && module.type === type)
.map(({ module }) => ({
text: module,
path: `/${module}`,
}));
}
export function makeFlatModuleList(
modules: Array<EnhancedModule>,
) {
return modules
.map((module) => ({
text: module.module,
path: `/${module.module}`,
sub: module.sub, // Include sub-navigation if present
}))
.sort((a, b) => a.text.localeCompare(b.text));
}
// Create hierarchical module list grouped by type and group
export function makeHierarchicalModuleList(
modules: Array<EnhancedModule>,
) {
const grouped: Record<string, Record<string, EnhancedModule[]>> = {};
modules.forEach((module) => {
grouped[module.type][module.group].push(module);
});
return Object.entries(grouped).map(([type, groups]) => ({
text: formatTypeTitle(type),
path: `/${type}`,
sub: Object.entries(groups).map(([group, moduleList]) => ({
text: formatGroupTitle(group),
path: `/${type}/${group}`,
sub: moduleList
.sort((a, b) => a.module.localeCompare(b.module))
.map((module) => ({
text: module.module,
path: `/${module.module}`,
sub: module.sub,
})),
})),
}));
}
function formatTypeTitle(type: string): string {
return type.charAt(0).toUpperCase() + type.slice(1);
}
function formatGroupTitle(group: string): string {
return group
.split('_')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
}