forked from RocketChat/docs-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-broken-links.js
73 lines (65 loc) · 1.55 KB
/
find-broken-links.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
// List all files in a directory in Node.js recursively in a synchronous fashion
var fs = require('fs');
var path = require('path');
function walkSync(dir, cbFile) {
files = fs.readdirSync(dir);
files.forEach(function(file) {
file = path.join(dir, file);
if (fs.statSync(file).isDirectory()) {
walkSync(file, cbFile);
} else {
cbFile(file);
}
});
};
walkSync(__dirname, function(file) {
if (/\.md$/.test(file) === false) {
return;
}
var text = fs.readFileSync(file).toString();
var links = [];
// text.replace(/\[(.*?)\]\((.*?)\)/gm, function(link, text, url) {
// if (/^https?:\/\/.+/.test(url)) {
// // TODO
// } else if (/mailto:.+/.test(url)) {
// // TODO
// } else if (/#.+/.test(url)) {
// // TODO
// } else {
// // console.log(url);
// url = decodeURIComponent(url);
// if (/^\//.test(url)) {
// var link2 = path.join(__dirname, url)
// } else {
// var link2 = path.join(path.dirname(file), url)
// }
// try {
// fs.statSync(link2)
// } catch (error) {
// links.push(url);
// }
// }
// })
text.replace(/\[(\d+)\]:(.+)/gm, function(link, text, url) {
// [3]:../13.%20Update%20Message
// console.log(url);
url = decodeURIComponent(url);
if (/^\//.test(url)) {
var link2 = path.join(__dirname, url)
} else {
var link2 = path.join(path.dirname(file), url)
}
try {
fs.statSync(link2)
} catch (error) {
links.push(url);
}
});
if (links.length) {
console.log(file);
links.forEach(function(link) {
console.log(' ', link);
});
console.log();
}
});