-
Notifications
You must be signed in to change notification settings - Fork 109
/
sidebars.js
75 lines (70 loc) · 2.14 KB
/
sidebars.js
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
const path = require('path');
const glob = require('glob');
function markdownFiles() {
return glob.sync('docs/api/*/*.md', { cwd: __dirname, absolute: true, root: '' });
}
function titleCase(str) {
return str.toLowerCase().replace(/\b(\w)/g, (s) => s.toUpperCase());
}
function nestMarkdownFiles() {
return markdownFiles()
.sort()
.sort((a, b) => a.split('.').length - b.split('.').length)
.reduce((acc, f) => {
const url = path.relative(path.join(__dirname, 'docs'), f).replace(/\.md$/, '');
const category = titleCase(url.split('/')[1]);
const basename = path.basename(url);
const parts = basename.split('.');
if (parts.length === 1) {
return [
...acc,
{
type: 'category',
label: basename,
link: { type: 'doc', id: `api/namespaces/${basename}` },
items: [],
},
];
}
// Find the right place to insert this item
const items = parts
.slice(0, -1) // Omit the last part, it'll be inserted below
.reduce((items, part) => items.find((item) => typeof item === 'object' && item.label === part).items, acc);
// Insert new item
if (category === 'Namespaces') {
items.push({
type: 'category',
label: url.replace(/.*\./, ''),
link: { type: 'doc', id: `api/namespaces/${basename}` },
items: [],
});
} else {
let item = items.find(({ label }) => label === category);
if (item === undefined) {
const anchor = category.toLowerCase() === 'enums' ? 'enumerations' : category.toLowerCase();
item = {
type: 'category',
label: category,
items: [],
customProps: {
breadcrumbLink: `/api/namespaces/${parts[0]}#${anchor}`,
},
};
items.push(item);
}
item.items.push(url);
}
return acc;
}, []);
}
module.exports = {
referenceSidebar: [
'index',
{
type: 'category',
label: 'API',
link: { type: 'doc', id: 'api/index' },
items: nestMarkdownFiles(),
},
],
};