-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathgenMd.js
More file actions
94 lines (85 loc) · 2.39 KB
/
genMd.js
File metadata and controls
94 lines (85 loc) · 2.39 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const fs = require('fs')
const path = require('path')
var readline = require('readline');
function readDirList(basePath, dataList, excludes) {
var pa = fs.readdirSync(basePath);
pa.forEach((it, idx) => {
if (excludes.indexOf(it) > -1) {
return;
}
let filePath = basePath + "/" + it;
var info = fs.statSync(filePath)
var itData = {
name: it,
path: filePath,
child: []
};
if (info.isDirectory()) {
itData.dirFlag = true;
dataList.push(itData);
readDirList(filePath, itData.child, excludes);
} else if (it.toLowerCase().endsWith(".md")) {
itData.dirFlag = false;
dataList.push(itData);
}
})
}
async function readFirstLine(path) {
return new Promise((resolve, reject) => {
var fRead = fs.createReadStream(path);
var objReadline = readline.createInterface({
input: fRead
});
objReadline.on('line', function (line) {
if (line && line.trim() != '') {
resolve(line);
}
});
})
}
function genEmptyStrs(nums) {
var res = [];
for (var i = 0; i < nums; i++) {
res.push(" ");
}
return res.join("");
}
async function genMuluStr(data, depths, resList) {
if (!data) {
return;
}
var name = '';
if (data.dirFlag) {
name = data.name;
} else if (data.name.toLowerCase().endsWith('readme.md')) {
name = await readFirstLine(data.path);
name = name.replace('#', '').trim();
} else {
name = data.name.replace('.md', '');
}
let str = data.dirFlag ? `* ${name}` : `* [${name}](${data.path.replace('./', '')})`;
resList.push(genEmptyStrs(depths * 2) + str);
depths++;
const child = data.child;
if (!child || !child.length) {
return;
}
for (var i of child) {
await genMuluStr(i, depths, resList);
}
}
async function main() {
var basePath = ".";
var dataList = [];
let excludes = ['.git', '.idea'];
readDirList(basePath, dataList, excludes);
var resList = [];
console.log('data:', JSON.stringify(dataList, null, 2));
for (var it of dataList) {
// 遍历所有文件和文件夹
var idx = 0;
await genMuluStr(it, idx, resList);
}
console.log(resList.join('\n'));
}
main();