forked from visualdiffer/visualdiffer.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_anchor.js
More file actions
32 lines (28 loc) · 668 Bytes
/
add_anchor.js
File metadata and controls
32 lines (28 loc) · 668 Bytes
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
const readline = require('readline');
const fs = require('fs');
const readInterface = readline.createInterface({
input: fs.createReadStream('unixshell.md'),
// output: process.stdout,
console: false
});
let prevLine = null;
readInterface.on('line', function(line) {
if (line[0] === '=') {
if (prevLine) {
const anchor = prevLine.toLowerCase().replace(/[^a-z0-9]+/g, '_');
console.log(`[${prevLine}](#${anchor})`);
prevLine = null;
}
console.log(line);
} else {
if (prevLine !== null) {
console.log(prevLine);
}
prevLine = line;
}
});
readInterface.on('close', function(line) {
if (prevLine) {
console.log(prevLine);
}
});