-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate-links.mjs
66 lines (59 loc) · 1.73 KB
/
validate-links.mjs
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
import { unified } from "unified";
import { visit } from "unist-util-visit";
import parse from "remark-parse";
import stringify from "remark-stringify";
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
// Obtain the target file
const [, , file] = process.argv;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
if (!file) {
throw new Error("Provide the target file");
}
console.log(`Reviewing file: ${file}`);
// Obtain the markdown contents
const sourceFileResolved = path.resolve(__dirname, file);
const contents = fs.readFileSync(sourceFileResolved);
function validator() {
return transformer;
function transformer(tree) {
visit(tree, "link", function (node) {
const { url } = node;
console.log(`Validating url: ${url}`);
// Local reference
if (
/^(?!www\.|(?:http|ftp|mailto)s?:(\/\/)?|[A-Za-z]:\\|\/\/).*/.test(
url
) &&
!/^#/.test(url)
) {
// Clean the URL
const cleanUrl = url.replace(/%20/g, " ").replace(/#.+/, "");
// Create a resolved path
const resolvedLinkPath = (() => {
if (/\//.test(cleanUrl)) {
return path.resolve(__dirname, `./${cleanUrl}`);
}
return path.resolve(path.resolve(sourceFileResolved, ".."), cleanUrl);
})();
if (!fs.existsSync(resolvedLinkPath)) {
console.error(`Link unresolveable: ${url} - ${resolvedLinkPath}`);
process.exit(255);
}
}
});
}
}
// Parse the AST
const tree = unified()
.use(parse)
.use(validator)
.use(stringify)
.process(contents, function (err) {
if (err) {
console.error(err);
}
process.exit(0);
});