-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathextract-docs.js
More file actions
68 lines (63 loc) · 2.12 KB
/
extract-docs.js
File metadata and controls
68 lines (63 loc) · 2.12 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
const { join } = require('path');
const { resolve: urlResolve } = require('url');
const fs = require('fs');
const baseURL = 'https://alova.js.org/';
const config = {
basePath: [
'i18n/zh-CN/docusaurus-plugin-content-docs/current/tutorial/02-getting-started',
'i18n/zh-CN/docusaurus-plugin-content-docs/current/tutorial/03-client/01-strategy/04-use-pagination.md'
],
exclude: ['_category_.json', 'example', 'congratulations']
};
const replaceTargetContent = (content, link) => {
const buildModule = mat => `++++++++++\nDocument link: ${link}\n${mat}`;
return (
content
// 章节标题改为一级标题
.replace(/---\s+?title:([\s\S]*?)\n[\s\S]*?---/g, (_, rep) => buildModule(rep))
// 添加分隔符
.replace(/[^#]##\s+(.*?)\s/g, mat => buildModule(mat))
// 替换链接
.replace(/\(\/((category|tutorial|api|contributing|resource).+?)\)/g, `(${baseURL}$1)`)
);
};
const buildLink = filesrc =>
urlResolve(baseURL, filesrc.split('docs')[1])
.replace('current/', '')
.replace(/[0-9]+-/g, '')
.replace('.README.md', '')
.replace('.md', '');
const extract = async targetPath => {
let filepaths = [];
if (fs.statSync(targetPath).isFile()) {
filepaths = [targetPath];
} else {
filepaths = fs.readdirSync(targetPath).map(pathItem => join(targetPath, pathItem));
}
let distFileContent = '';
for (let filesrc of filepaths) {
if (config.exclude.some(item => filesrc.includes(item))) {
continue;
}
let stats = fs.statSync(filesrc);
if (stats.isFile()) {
const docLink = buildLink(filesrc);
distFileContent += replaceTargetContent(
fs.readFileSync(filesrc, { encoding: 'utf8' }),
docLink
);
distFileContent += '\n\n\n';
} else {
distFileContent += await extract(filesrc);
}
}
return distFileContent;
};
(async () => {
const basePath = Array.isArray(config.basePath) ? config.basePath : [config.basePath];
let content = '';
for (const path of basePath) {
content += await extract(join(process.cwd(), path));
}
fs.writeFileSync(join(process.cwd(), './distFile.md'), content);
})();