forked from apachecn/doctool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tomd.js
44 lines (37 loc) · 1000 Bytes
/
tomd.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
var toMD = require('to-markdown');
var fs = require('fs');
var myConventors = require('./my-conventors');
var path = require('path')
var isDir = s => fs.statSync(s).isDirectory()
function main() {
var fname = process.argv[2]
if(!fname) {
console.log('请指定文件或目录。')
process.exit();
}
if(isDir(fname))
processDir(fname)
else
processFile(fname)
}
function processDir(dir) {
var files = fs.readdirSync(dir)
for(var fname of files) {
fname = path.join(dir, fname)
processFile(fname)
}
}
function processFile(fname) {
if(!fname.endsWith('.html'))
return;
console.log(`file: ${fname}`)
var co = fs.readFileSync(fname, 'utf-8')
.replace(/<a[^>]*\/>/g, '');
var mdName = fname.replace(/\.html/g, '.md')
var md = toMD(co, {
gfm: true,
converters: myConventors,
});
fs.writeFileSync(mdName, md);
}
if(module == require.main) main()