forked from jaywcjlove/linux-command
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawler.js
More file actions
65 lines (49 loc) · 1.88 KB
/
Copy pathcrawler.js
File metadata and controls
65 lines (49 loc) · 1.88 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
var request = require('superagent');
var cheerio = require('cheerio');
var toMarkdown = require('to-markdown');
var path = require('path');
var fs = require('fs');
var param = process.argv.slice(2);
var from_path = param[0];
var to_path = param[1];
if(!from_path) return console.error("请输入请求参数!");
if(!to_path) return console.error("请输入写入目标目录!");
CreatMarkdown(from_path,to_path)
function CreatMarkdown(from_path,to_path){
var new_to_path = path.join(path.dirname(__dirname),to_path)
if(exists(new_to_path)) return console.log(" → error: 目录存在 ",to_path,'\n')
new_to_path = path.dirname(new_to_path)
mkdirsSync(new_to_path,0777,function(){
request.get(from_path).end(function(err, res){
// console.log("to_path::",to_path)
var md_str = res.text
md_str = md_str.replace(/<pre>/gi,'```\n')
md_str = md_str.replace(/<\/pre>/gi,'\n```')
md_str = md_str.replace(/<span.*?>/gi,'')
md_str = md_str.replace(/<\/span>/gi,'')
md_str = md_str.replace(/\[[^\]]*\]\(.*?\)/g,function(str){
str.replace(/\[(.*?)\]/,'');
return RegExp.$1;
})
fs.writeFileSync(to_path, toMarkdown(md_str).toString() ,'utf-8');
console.log(" → ",to_path)
});
});
}
// 同步循环创建所有目录 resolvePath
function mkdirsSync(dirpath, mode, callback) {
if(fs.existsSync(dirpath)){
callback&&callback(dirpath);
return true;
}else{
if(mkdirsSync(path.dirname(dirpath), mode)){
fs.mkdirSync(dirpath, mode, callback);
callback&&callback(dirpath);
return true;
}else{
callback&&callback(dirpath);
}
}
};
//检查指定路径的文件或者目录,是否存在
function exists(_path){return fs.existsSync(_path);}