-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidebars.ts
More file actions
186 lines (148 loc) Β· 4.53 KB
/
Copy pathsidebars.ts
File metadata and controls
186 lines (148 loc) Β· 4.53 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// [<] i think this code could be cleaner but then again my expertise
// [<] isn't JS/TS so im sorry if this triggers anyone or something
import fs from 'node:fs';
import path from 'node:path';
interface ApiNode {
name: string,
children: Record<string, ApiNode>,
docId: string | null,
_cachedItems?: SidebarItem[]
}
interface CategoryItem {
type: 'category',
label: string,
collapsed: boolean,
link?: { type: 'doc', id: string },
items: SidebarItem[]
}
type SidebarItem = string | CategoryItem;
function frameworkSortKey(fw: string): { tier: number; value: number; } {
const version = fw.slice(3); // [i] we couldn't care less about the "net" part
if (version.includes('.')) {
// [i] just a float conversion
return { tier: 0, value: Number.parseFloat(version) || 0 };
}
// [i] we treat PATCH as part of MINOR
const major = version.slice(0, 1);
const rest = version.slice(1);
return { tier: 1, value: Number.parseFloat(`${major}.${rest}`) };
}
function compareFrameworks(a: string, b: string) {
const ka = frameworkSortKey(a);
const kb = frameworkSortKey(b);
if (ka.tier !== kb.tier)
return ka.tier - kb.tier;
return kb.value - ka.value;
}
function compareStrings(a: string, b: string): number {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
function generateApiSidebar(): CategoryItem[] {
const apiDir = path.resolve(__dirname, 'docs/api');
if (!fs.existsSync(apiDir)) return [];
const frameworks = fs
.readdirSync(apiDir, { withFileTypes: true })
.filter(d => d.isDirectory())
.map(d => d.name)
.sort(compareFrameworks);
const results: CategoryItem[] = [];
for (const fw of frameworks) {
const fwDir = path.join(apiDir, fw);
if (!fs.existsSync(fwDir)) continue;
const files = fs
.readdirSync(fwDir)
.filter(f => f.endsWith('.md'))
.map(f => f.replace(/\.md$/, ''));
const root = buildNamespaceTree(files, fw);
const assemblyNode = findAssemblyRoot(root);
const category: CategoryItem = {
type: 'category',
label: formatFrameworkLabel(fw),
collapsed: fw !== frameworks[0],
link: assemblyNode.docId ? { type: 'doc', id: assemblyNode.docId } : undefined,
items: childrenToItems(assemblyNode)
}
results.push(category);
};
return results;
}
function buildNamespaceTree(files: string[], fw: string) {
const root: ApiNode = {
name: '',
children: Object.create(null) as Record<string, ApiNode>,
docId: null
};
for (const fileName of files) {
const parts = fileName.split('.');
let node = root;
for (const part of parts) {
if (!node.children[part]) {
node.children[part] = {
name: part,
children: Object.create(null) as Record<string, ApiNode>,
docId: null,
};
}
node = node.children[part];
}
node.docId = `api/${fw}/${fileName}`;
}
return root;
}
function findAssemblyRoot(root: ApiNode): ApiNode {
let node = root;
while (true) {
const keys = Object.keys(node.children);
if (keys.length !== 1) break;
node = node.children[keys[0]];
}
return node;
}
function childrenToItems(node: ApiNode): SidebarItem[] {
if (node._cachedItems) return node._cachedItems;
const keys = Object.keys(node.children).sort(compareStrings);
const categoryItems: CategoryItem[] = [];
const typeItems: string[] = [];
for (const key of keys) {
const child = node.children[key];
const hasChildren = Object.keys(child.children).length > 0;
if (hasChildren) {
const items = childrenToItems(child);
if (items.length > 0 || child.docId) {
categoryItems.push({
type: 'category',
label: child.name,
collapsed: true,
link: child.docId ? { type: 'doc', id: child.docId } : undefined,
items,
});
} else {
console.warn('Skipping empty category for', child.name);
}
} else if (child.docId != null) {
typeItems.push(child.docId);
}
}
const result: SidebarItem[] = [...categoryItems, ...typeItems];
node._cachedItems = result;
return result;
}
function formatFrameworkLabel(fw: string) {
if (!fw.startsWith('net'))
return fw;
const version = fw.slice(3); // [i] ignore the net prefix ofc
if (version.includes('.'))
return `.NET ${version}`;
return `.NET Framework ${version.split('').join('.')}`;
}
const sidebars = {
bindingSidebar: [{ type: 'autogenerated', dirName: 'bindings' }],
tutorialSidebar: [{ type: 'autogenerated', dirName: 'tutorials' }],
languageSidebar: [{ type: 'autogenerated', dirName: 'language' }],
nativeSidebar: [{ type: 'autogenerated', dirName: 'native' }],
toolSidebar: [{ type: 'autogenerated', dirName: 'tools' }],
apiSidebar: generateApiSidebar()
};
export default sidebars;