-
Notifications
You must be signed in to change notification settings - Fork 123
/
formatMenu.js
36 lines (31 loc) · 916 Bytes
/
formatMenu.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
const fs = require('fs');
const {
join
} = require('path');
// fill in th missing fields: parentId, parentIds, href,
const formatMenuStructure = (list, parentId= '',parentIds=[]) => {
if(!list || !list.length) {
return []
}
list.forEach(item => {
const {
children,
id,
isMenu
} = item;
item.parentId = parentId;
item.parentIds = [...parentIds];
if(!isMenu) {
item.href = id
}
item.children = formatMenuStructure(children,id, [...parentIds, id])
})
return list
}
const legallyVersions = ['v2.4.x','v2.3.x', 'v2.2.x', 'v2.1.x','v2.0.x'];
const filePaths = legallyVersions.map(version => join(__dirname, `${version}/site/en/menuStructure/en.json`));
filePaths.forEach(filePath => {
const menu = fs.readFileSync(filePath, 'utf8');
const newMenu = formatMenuStructure(JSON.parse(menu));
fs.writeFileSync(filePath, JSON.stringify(newMenu))
})