-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·59 lines (56 loc) · 1.46 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env node
let CONST = {
"FILETYPE": "UTF-8",
// 第一个 ### 标题的 # 个数
"FIRSTHN": 2,
"TAB": " ",
// "TAB": "\t",
}
let uniqIds = [];
let indexStr = ["# 目录"];
let fs = require('fs');
if(!fs.existsSync(process.argv[2])){
console.log("文件不存在");
process.exit(1);
}
let data = fs.readFileSync(process.argv[2], CONST.FILETYPE);
data = data.split("\n");
console.log(data);
data.map((line,index)=>{
if(line.indexOf("#") === 0){
let newLine = getHn(line);
data[index] += "<a id=" + newLine.id + "></a>"
indexStr.push(genContent(newLine));
// console.log(genContent(getHn(line)));
}
});
let newData = indexStr.concat(data).join("\n");
fs.writeFileSync(process.argv[2], newData);
console.log("添加成功,共添加 ",indexStr.length - 1 ," 个目录" )
/**
* util 类型函数
*/
// 截取字段
function getHn(line){
let id = genId();
let result = {h: line.indexOf(" ") - line.indexOf("#"), t:line.substr(line.indexOf(" ")+1), id:id};
return result;
}
// 生成 id
function genId(){
let id = ("" + Math.random()).substr(2,5);
while(uniqIds.indexOf(id) > -1){
id = Math.random().substr(2,5);
}
uniqIds.push(id);
return id;
}
// 生成
function genContent(h){
let tabs = "";
for(let i = 0;i<h.h - CONST.FIRSTHN;i++){
// 可能使用两个空格
tabs += CONST.TAB;
}
return tabs + "- [" + h.t +"]" + "(#" + h.id + ")";
}