-
Notifications
You must be signed in to change notification settings - Fork 3
/
sidebar.js
111 lines (104 loc) · 3.13 KB
/
sidebar.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
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
const sidebarLabelOrder = {
'Getting Started': 0, // First
'Vocdoni SDK': 1,
'Vocdoni API': 2,
'UI Components': 3,
'Vocdoni Protocol': 4,
'Development Guidelines': 5, // Last
};
const sdkSidebarIDOrder = {
'sdk/reference/sdk-reference': -1, // First
// All other elements are 0 by default
'sdk/reference/changelog': 1, // Last
};
// Removes autogenerated API sidebar- this is broken.
function skipAPI(items) {
return items.filter(({label}) => {
return (
// Exclude vocdoni-api folder, this folder needs swagger-generated sidebar
label != 'vocdoni-api'
);
});
}
function sortItems(items) {
// Sort sidebar items by label according to sidebarLabelOrder
return items.sort((i1, i2) => {
return (
(sidebarLabelOrder[i1.label] || 0) - (sidebarLabelOrder[i2.label] || 0)
);
});
}
// Adds api sidebar items from './docs/vocdoni-api/sidebar.js', filters out index file
function addAPI(items) {
// Define root api sidebar item
const apiItem = {
type: 'category',
label: 'Vocdoni API',
link: {
type: 'doc',
id: 'vocdoni-api/vocdoni-api',
},
items: [],
};
// Get swagger-generated API sidebar items, filter out index doc
require('./docs/vocdoni-api/sidebar.js').forEach(item => {
if (item?.type !== 'doc' && item?.label !== 'Vocdoni API') {
apiItem.items.push(item);
}
});
items.push(apiItem);
return items;
}
function toTitleCase(str) {
return str.charAt(0).toUpperCase() + str.substr(1).toLowerCase();
}
function modifySDKReference(items) {
for (var index1 in items) {
if (
items[index1].type == 'category' &&
items[index1].label == 'Vocdoni SDK'
) {
const SDKCategory = items[index1];
for (var index2 in SDKCategory.items) {
if (
SDKCategory.items[index2]?.type == 'category' &&
SDKCategory.items[index2]?.label == 'reference'
) {
// Modify reference category: add link and capitalize label
referenceCategory = {
...SDKCategory.items[index2],
label: 'Reference',
link: {
type: 'doc',
id: 'sdk/reference/sdk-reference',
},
};
// Capitalize category labels
for (var index3 in referenceCategory?.items) {
if (referenceCategory.items[index3].label != null) {
referenceCategory.items[index3].label = toTitleCase(
referenceCategory.items[index3].label
);
}
}
// Sort sidebar items by ID according to sdkSidebarIDOrder
referenceCategory.items = referenceCategory.items.sort((i1, i2) => {
return (
(sdkSidebarIDOrder[i1.id] || 0) - (sdkSidebarIDOrder[i2.id] || 0)
);
});
SDKCategory.items[index2] = referenceCategory;
}
}
items[index1] = SDKCategory;
}
}
return items;
}
module.exports = async function sidebarItemsGenerator({
defaultSidebarItemsGenerator,
...args
}) {
const sidebarItems = await defaultSidebarItemsGenerator(args);
return modifySDKReference(sortItems(addAPI(skipAPI(sidebarItems))));
};