-
Notifications
You must be signed in to change notification settings - Fork 315
/
format.js
107 lines (85 loc) · 2.08 KB
/
format.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
const fs = require('fs');
const {
join
} = require('path');
const formatMenuStructure = list => {
const newList = list.map(v => {
const {
id,
title,
isMenu = false,
outLink = '',
order = 0,
label1,
label2,
label3,
} = v;
const parentId = label3 || label2 || label1 || '';
const parentIds = [label1, label2, label3].filter(v => !!v);
const level = [label1, label2, label3].filter(v => !!v).length + 1;
const baseInfo = {
id: id,
label: title,
}
if (isMenu) {
baseInfo.isMenu = true;
}
if (outLink) {
baseInfo.externalLink = outLink;
}
return {
...baseInfo,
parentId,
parentIds,
level,
order,
children: [],
};
});
newList.sort((x, y) => y.level - x.level);
const resultList = newList.slice();
newList.forEach(v => {
const {
parentId
} = v;
const parentIndex = resultList.findIndex(v => v.id === parentId);
if (parentIndex !== -1) {
let childInfo = {
label: v.label,
id: v.id,
// href: v.href,
// parentId: parentId,
// parentIds: v.parentIds,
// level: v.level,
order: v.order,
}
if(v.isMenu){
childInfo.isMenu = true;
}
if(v.externalLink){
childInfo.externalLink = v.externalLink
}
childInfo.children = v.children;
resultList[parentIndex].children.push(childInfo);
}
});
return resultList.filter(v => v.level === 1).map(v => ({
label: v.label,
id: v.id,
isMenu: v.isMenu,
externalLink: v.externalLink,
// href: v.href,
// parentId: parentId,
// parentIds: v.parentIds,
// level: v.level,
order: v.order,
children: v.children,
}));
};
const formatMenu = () => {
const menuPath = join(__dirname, 'site/en/menuStructure/en.json')
const menu = fs.readFileSync(menuPath, 'utf-8');
const newMenuStructure = formatMenuStructure(JSON.parse(menu).menuList);
fs.writeFileSync(menuPath, JSON.stringify(newMenuStructure));
};
formatMenu();