forked from aslushnikov/table-of-contents-preprocessor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoc.js
executable file
·97 lines (81 loc) · 2.87 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env node
var fs = require('fs');
var program = require('commander');
program.version("0.9.0")
.option("-d, --depth <n>", "Specifies the maximal header depth for the TOC", parseInt)
.option("-a, --alternativeLinks", "Use alternative links for headings.\n" +
"\tE.g. 1.1. Foo will become 1-1-foo instead of 11-foo. This option also\n" +
"\tstrips all non-Latin and non-numeric characters from the URLs. Useful for\n" +
"\tsome Markdown flavors like the GitLab Flavored Markdown")
.parse(process.argv);
var FOUR_SPACES = " ";
var leftIndents = [""];
if(process.argv.length < 3) {
program.help();
}
for(var i = 1; i < 10; i++) {
leftIndents.push(leftIndents[i-1] + FOUR_SPACES);
}
fs.readFile(process.argv[process.argv.length-1], '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 inCodeBlock = false;
var minDepth = 1000000;
for(var i = 0; i < lines.length; i++) {
var line = lines[i];
var codeBlockBeginOrEnd = line.match(/^```.*$/);
if (codeBlockBeginOrEnd) inCodeBlock = !inCodeBlock;
var headingLine = line.match(/^(#+)(.*)\s*$/);
if (!headingLine || inCodeBlock) continue;
minDepth = Math.min(minDepth, headingLine[1].length);
addHeadingLine(headingLine, depths, titles);
}
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 addHeadingLine(headingLine, depths, titles) {
if(program.depth >= headingLine[1].length || !program.depth) {
depths.push(headingLine[1].length);
titles.push(headingLine[2]);
}
}
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) {
var trimmedTitle = title.trim()
.replace(/[a-z]+/ig, function(match) {
return match.toLowerCase();
});
if (program.alternativeLinks) {
return trimmedTitle.replace(/[^0-9a-z]+/ig, '-');
} else {
return trimmedTitle.replace(/\s/g, '-')
.replace(/[^-0-9a-zа-яё]/ig, '');
}
}
function tocLine(depth, title) {
return leftIndents[depth] + "- [" + title.trim() + "](#" + titleToUrl(title) + ")";
}