-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
31 lines (28 loc) · 849 Bytes
/
main.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
/**
* Getting Started
*
* node main.js test.md result.md
*/
var fs = require('fs')
fs.readFile(process.argv[2], 'utf8', function (err, markdown) {
if (err) {
return console.log(err);
}
var counter = 1;
var matches = {};
var matcher = /\[.*?\]\((.*?)\)/g;
while (match = matcher.exec(markdown)) {
if (!matches[match[1]]) matches[match[1]] = counter++;
}
Object.keys(matches).forEach(function(url) {
var number = matches[url];
// uncomment below and comment above for numbering in reverse
//var number = Object.keys(matches).length - matches[url] + 1;
var r = new RegExp("(\\[.*?\\])\\(" + url + "\\)", "g");
markdown = markdown.replace(r, "$1[" + number + "]");
markdown += "\n[" + number + "]: " + url;
});
fs.writeFile(process.argv[3], markdown, 'utf8', function (err) {
if (err) return console.log(err);
});
});