-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgenerate-guides.js
86 lines (75 loc) · 2.18 KB
/
generate-guides.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
const fm = require('front-matter');
const fs = require('fs-extra');
const path = require('path');
const docsDir = path.resolve(__dirname, '..', 'docs');
const src = path.join(docsDir, 'guide');
const dest = path.join(docsDir, '.vuepress', 'guide.json');
function walkSection(dir, stripTitlePrefix, parent) {
const items = [];
for (const filename of fs.readdirSync(dir)) {
const file = path.join(dir, filename);
const isDir = fs.statSync(file).isDirectory();
const rel = `/${path.relative(docsDir, file)}/`;
const name = filename.replace(/\.md$/, '');
let title = name;
let weight = 10;
const mdFile = isDir ? path.join(file, 'README.md') : file;
if (!/\.md$/.test(mdFile) || filename === 'README.md') {
// skip non-markdown files and skip readme since the parent dir already processed it
continue;
}
try {
const { attributes } = fm(fs.readFileSync(mdFile, 'utf8'));
if (attributes.title) {
title = attributes.title;
}
if (attributes.sidebar_title) {
title = attributes.sidebar_title;
}
if (attributes.weight) {
weight = parseInt(attributes.weight);
}
} catch (e) {}
if (stripTitlePrefix && title.startsWith(stripTitlePrefix)) {
title = title.substring(stripTitlePrefix.length);
}
items.push({
name,
title,
weight,
file,
path: rel,
isDir
});
}
return items.sort((a, b) => a.weight - b.weight).map(item => {
if (item.isDir) {
const children = walkSection(item.file, stripTitlePrefix, `${parent ? `${parent}/` : ''}${item.name}`);
if (children.length) {
return {
title: item.title,
path: item.path,
children
};
}
}
return [ `${parent ? `${parent}/` : ''}${item.name}${item.isDir ? '/' : ''}`, item.title ];
});
}
const titleMap = {
'Alloy_Framework': 'Alloy ',
'Titanium_SDK' : 'Titanium SDK '
}
function walk(dir) {
const result = {};
for (const name of fs.readdirSync(dir)) {
const file = path.join(dir, name);
if (fs.statSync(file).isDirectory()) {
result[`/${path.relative(docsDir, file)}/`] = walkSection(file, titleMap[name]);
}
}
return result;
}
const guide = walk(src);
fs.writeFileSync(dest, JSON.stringify(guide, null, 4), 'utf8');
console.log(`Wrote ${dest}`);