-
Notifications
You must be signed in to change notification settings - Fork 5
/
tocgen.js
55 lines (52 loc) · 1.14 KB
/
tocgen.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
"use strict"
// This file generates TOCs for all the documentation files that want it.
var toc = require('markdown-toc')
var fs = require('fs')
var uslug = require('uslug')
function readRecursive(dir, callback) {
fs.readdir(dir, (err, files) => {
files.forEach(val => {
if (val == ".git" || val == "node_modules")
return
fs.lstat(dir + "/" + val, (err, stats) => {
if (err) {
console.log(err)
return
}
if (stats.isFile() && val.slice(-3) == ".md") {
callback(dir + "/" + val)
}
else if (stats.isDirectory())
readRecursive(dir + "/" + val, callback)
})
})
})
}
function slugger(w) {
return uslug(w, {
allowedChars: ":-_~/()"
})
}
readRecursive(".", (file) => {
console.log("=> ", file)
fs.readFile(file, (err, data) => {
data = data.toString()
if (err) {
console.log(err)
return
}
var newData = toc.insert(data, {
filter: (str, ele, arr) => {
// remove []
return str.replace(/[\[\]]/ig, "")
},
slugify: slugger,
bullets: ["*"],
maxdepth: 3
})
fs.writeFile(file, newData.trimRight() + "\n", (err) => {
if (err)
console.log(file, err)
})
})
})