forked from alibaba/ice
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-docs-database.js
More file actions
executable file
·75 lines (64 loc) · 1.67 KB
/
generate-docs-database.js
File metadata and controls
executable file
·75 lines (64 loc) · 1.67 KB
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
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const glob = require('glob');
const markTwain = require('mark-twain');
const { cut } = require('./participle');
const destDir = path.join(__dirname, '../build');
const dest = path.join(destDir, 'docs.json');
const sourceDir = path.join(__dirname, '../docs');
glob(
'**/*.md',
{
nodir: true,
ignore: 'README.md',
cwd: sourceDir,
},
(err, files) => {
if (err) {
throw err;
}
const result = [];
files.forEach((file) => {
const content = fs.readFileSync(path.join(sourceDir, file), 'utf-8');
const jsonML = markTwain(content);
// 隐藏文档过滤
if (jsonML.meta.hide) {
return;
}
if (!jsonML.meta.title) {
throw new Error(`${file} 缺失标题, 请补充`);
}
// 分词 payload
const participle = {
title: cut(jsonML.meta.title),
content: cut(content),
};
const sourceData = {
filename: file,
path: file.replace(/\.md$/, ''),
...jsonML.meta,
participle,
jsonml: jsonML.content,
};
result.push(sourceData);
});
fs.writeFileSync(dest, JSON.stringify(result, null, 2), 'utf-8');
console.log('文档数据生成完毕. Docs DB Generated.');
console.log(dest);
}
);
/**
* 从 jsonml 结构中获取纯字符串
*/
function getJsonmlString(jsonml) {
let result = '';
for (let i = 1; i < jsonml.length; i++) {
if (Array.isArray(jsonml[i])) {
result += getJsonmlString(jsonml[i]);
} else if (typeof jsonml[i] === 'string') {
result += jsonml[i] + ' ';
}
}
return result;
}