-
Notifications
You must be signed in to change notification settings - Fork 4
/
replace-anchor.js
executable file
·37 lines (31 loc) · 1.15 KB
/
replace-anchor.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
#!/usr/bin/env node
var
fs = require('fs'),
readline = require('readline'),
stream = require('stream'),
regex = /(<a href=\")([A-Za-z0-9\(\):\/._-]+)\" target=\"_blank\">([\w\s\.\(\)-]+)(<\/a>)/, // TODO find representation for " target=\_blank\">
instream = fs.createReadStream('README.md'),
outstream = new stream,
rl = readline.createInterface(instream, outstream);
var output = "";
rl.on('line', function(line) {
var match = regex.exec(line);
while (match) {
var markdown = "[" + match[3] + "]" + "(" + match[2] + ")";
line = line.slice(0, match.index) + markdown + line.slice(match.index + match[0].length);
match = regex.exec(line);
}
output += line + "\n";
});
rl.on('close', function() {
// remove TOC and installtion notes //
var indexTOC = output.search("\n-");
var indexLessonSteps = output.search("# Lesson Steps");
var header = output.slice(0, indexTOC);
var body = output.slice(indexLessonSteps);
output = header + body;
fs.writeFile('README.md', output, function (err) {
if (err) throw err;
console.log('Saved non-github markdown!');
});
});