forked from ant-design/ant-design-pro-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatMenu.js
41 lines (41 loc) · 1.21 KB
/
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
37
38
39
40
41
const fs = require('fs');
const path = require('path');
const fm = require('front-matter');
const dirPath = path.join(__dirname, '/docs/docs');
const filesPath = fs.readdirSync(dirPath);
const menu = {};
filesPath.forEach((filePath) => {
const data = fs.readFileSync(path.join(dirPath, filePath), 'utf8');
let { attributes } = fm(data);
const { nav, group, title, order } = attributes;
let targetPath = '';
if (filePath.includes('en-US')) {
targetPath = nav.path;
} else {
targetPath = `/zh-CN${nav.path}`;
}
if (!menu[targetPath]) {
menu[targetPath] = [];
}
let groups = menu[targetPath];
let resultGroup = groups.filter((item) => {
return item.title === group.title;
})[0];
if (!resultGroup) {
resultGroup = {
title: group.title,
children: [],
order,
};
groups.push(resultGroup);
}
resultGroup.children.push({ order, path: `docs/${filePath}` });
resultGroup.order = Math.min(resultGroup.order, order);
});
Object.keys(menu).forEach((key) => {
menu[key].sort((a, b) => a.order - b.order);
menu[key].forEach((group) => {
group.children = group.children.sort((a, b) => a.order - b.order).map((child) => child.path);
});
});
module.exports = menu;