forked from aslushnikov/table-of-contents-preprocessor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoc.js
executable file
·71 lines (58 loc) · 1.64 KB
/
toc.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
var fs = require('fs');
var FOUR_SPACES = " ";
var leftIndents = [""];
for(var i = 1; i < 10; i++) {
leftIndents.push(leftIndents[i-1] + FOUR_SPACES);
}
fs.readFile(process.argv[2], 'utf-8', function(err, data) {
if (err) {
throw err;
}
processData(data);
});
function processData(data) {
var lines = data.trimRight().split('\n');
var titles = [];
var depths = [];
var minDepth = 1000000;
for(var i = 0; i < lines.length; i++) {
var line = lines[i];
var m = line.match(/^(#+)(.*)\s*$/);
if (!m) continue;
minDepth = Math.min(minDepth, m[1].length);
depths.push(m[1].length);
titles.push(m[2]);
}
for(var i = 0; i < depths.length; i++) {
depths[i] -= minDepth;
}
var toc = createTOC(depths, titles).join('\n');
var tocRegexp = /^\s*@@TOC@@\s*$/;
for(var i = 0; i <lines.length; i++) {
var line = lines[i];
if (tocRegexp.test(line)) {
lines[i] = toc;
}
}
console.log(lines.join('\n'));
//console.log('\n');
}
function createTOC(depths, titles) {
var ans = [];
for(var i = 0; i < depths.length; i++) {
ans.push(tocLine(depths[i], titles[i]));
}
return ans;
}
function titleToUrl(title) {
return title.trim()
.replace(/[a-z]+/ig, function(match) {
return match.toLowerCase();
})
.replace(/\s/g, '-')
.replace(/[^-0-9a-zа-яё]/ig, '');
}
function tocLine(depth, title) {
return leftIndents[depth] + "- [" + title.trim() + "](#" + titleToUrl(title) + ")";
}