-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkChecker.js
63 lines (60 loc) · 1.46 KB
/
linkChecker.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
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
const input = [];
rl.on('line', line => {
input.push(line);
});
rl.on('close', () => {
const linkErrors = JSON.parse(input.join('\n'));
// We're not interested in anchor links. Mainly because bygg21.no has implemented them wrong, making it return errors all the time.
const errors = linkErrors.stats.errors.filter(
error => error.type !== 'remote-anchor'
);
// If there are no errors, we stop here.
if (!errors.length) {
return;
}
const stream = fs.createWriteStream('public/broken-links.html');
stream.once('open', () => {
const html = `
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-size: 16px;
font-family: sans-serif;
}
li {
margin-bottom: 1rem;
}
</style>
</head>
<body>
<h1>Sider med døde lenker 🔗👻</h1>
<ul>
${errors
.map(
error => `
<li>
Side: <a href="${error.source}">${error.source}</a>
<br />
Lenke: <a href="${error.target}">${error.target}</a>
<br />
Feilmelding: ${error.reason}
</li>
`
)
.join('')}
</ul>
</body>
</html>
`;
stream.end(html);
});
});