forked from graphile/graphile.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink-check.js
More file actions
77 lines (72 loc) · 1.89 KB
/
link-check.js
File metadata and controls
77 lines (72 loc) · 1.89 KB
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
74
75
76
77
/* eslint-disable no-console */
const fs = require("fs");
const path = require("path");
const glob = require("glob");
const chalk = require("chalk");
const base = `${__dirname}/public`;
const files = glob.sync(`${base}/**/*.html`);
const validLinks = files.map(f =>
f.substr(base.length).replace(/index.html$/, "")
);
function allMatches(str, regex) {
const all = [];
// eslint-disable-next-line no-constant-condition
while (true) {
const matches = regex.exec(str);
if (!matches) break;
all.push(matches[1]);
}
return all;
}
let invalid = 0;
for (const file of files) {
const filePretty = chalk.bold(
path
.relative(__dirname, file)
.replace(/^public/, "")
.replace(/index.html$/, "")
);
const contents = fs.readFileSync(file, "utf8");
if (contents.indexOf("Postgraphile") >= 0) {
invalid++;
console.error(
`${filePretty} mentions 'Postgraphile'; please change to 'PostGraphile' or 'postgraphile' for consistency.`
);
}
const links = allMatches(contents, /<a[^>]+href="([^"]+)"/g);
for (const link of links) {
const trimmed = link.replace(/[?#].*$/, "");
if (trimmed.match(/\.(js|css|png|svg|webmanifest)$/)) {
// Meh, resources
continue;
}
if (
trimmed.match(/^(https?:)?\/\//) &&
(trimmed.match(/github\.com/) ||
!trimmed.match(/graphile\.(org|meh)/) ||
trimmed.match(/learn\.graphile\.org/))
) {
// Absolute, continue
continue;
}
if (trimmed.match(/^mailto:/)) {
// mailto:, continue
continue;
}
if (trimmed === "") {
// Anchor link
continue;
}
if (validLinks.indexOf(trimmed) >= 0) {
// Cool, looks legit
continue;
}
invalid++;
console.error(`${filePretty} has invalid link to '${link}'`);
}
}
if (invalid > 0) {
console.log();
console.log(`${invalid} errors found 😔`);
process.exit(1);
}