-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidebar.config.js
149 lines (126 loc) · 3.89 KB
/
sidebar.config.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
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
const fs = require('fs');
const path = require('path');
const matter = require('gray-matter');
function getJson(file) {
const configJsonPath = path.join(__dirname, file);
const data = fs.readFileSync(configJsonPath, 'utf8');
return JSON.parse(data);
}
function cleanPath(path) {
let newPath;
newPath = path.replace(/^\/?docs\//, '');
newPath = newPath.replace(/^\//, '');
newPath = newPath.replace(/\.(mdx|md)$/, '');
return newPath;
}
function processPage(item, outputArray) {
if (typeof item === 'string') {
if (item.startsWith('reference/')) {
const cleanedPath = cleanPath(item);
const possibleEndings = ['.api.mdx', '.info.mdx', '.mdx'];
let mdxPath;
let mdxContent;
let metadata;
// Try each possible file ending until we find one that exists
for (let ending of possibleEndings) {
const pathToTry = path.join('docs', `${cleanedPath}${ending}`);
if (fs.existsSync(pathToTry)) {
mdxPath = pathToTry;
// Read the MDX file and extract metadata
mdxContent = fs.readFileSync(mdxPath, 'utf8');
metadata = matter(mdxContent).data;
break;
}
}
if (!mdxContent) {
// Handle the case where no file was found
throw new Error(`MDX file not found for path: ${cleanedPath}`);
}
// Add the className based on the sidebar_class_name
outputArray.push({
type: 'doc',
id: cleanedPath,
className: metadata.sidebar_class_name || 'wd_no_class_item'
});
} else {
// Simple page
outputArray.push({
type: 'doc',
id: cleanPath(item)
});
}
} else if (typeof item === 'object') {
const category = {
type: 'category',
label: item.groupName,
};
if (item.page) {
category['link'] = {
type: 'doc',
id: cleanPath(item.page)
};
}
category['items'] = [];
item.subpages.forEach(subItem => {
processPage(subItem, category.items);
});
outputArray.push(category);
}
}
function saveNewSidebar(sidebars, outputPath) {
const outputContent = `module.exports = ${JSON.stringify(sidebars, null, 2)};`;
fs.writeFile(outputPath, outputContent, 'utf8', (err) => {
if (err) {
console.error('Error writing the output file:', err);
}
else {
console.log(`[SIDEBAR] Sidebar generated\n`);
}
});
}
function transformSidebar(inputPath, outputPath) {
fs.readFile(inputPath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the input file:', err);
return;
}
try {
const file = JSON.parse(data);
const { sidebars } = file;
const newSidebars = {};
sidebars.forEach((sidebar) => {
newSidebars[sidebar.sidebarRef] = [];
sidebar.categories.forEach((category, index) => {
const { categoryName } = category;
const categoryTitle = {
type: 'category',
label: categoryName,
collapsed: false,
collapsible: false,
items: [],
};
if (index === 0) {
categoryTitle['className'] = 'wd_sidebar_first_item';
} else {
categoryTitle['className'] = 'wd_sidebar_item'
}
newSidebars[sidebar.sidebarRef].push(categoryTitle);
category.pages.forEach((item) => {
processPage(item, categoryTitle.items);
});
})
})
const planConfig = getJson('plan.json');
const { plan } = planConfig;
if (plan === 'free') {
saveNewSidebar({ "apiReference": Object.values(newSidebars)[0] }, outputPath)
} else {
saveNewSidebar(newSidebars, outputPath);
}
} catch (parseError) {
console.error('Error parsing the JSON data:', parseError);
}
});
}
transformSidebar('config.json', 'sidebars.js');
module.exports = transformSidebar;