-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathcheck-docs-link.js
71 lines (57 loc) · 1.71 KB
/
check-docs-link.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
'use strict';
const fs = require('fs');
const childProcess = require('child_process');
const path = require('path');
const fetch = require('make-fetch-happen');
const { JSDOM } = require('jsdom');
async function main() {
const { stdout } = childProcess.spawnSync('git', ['ls-files']);
let links = [];
const files = stdout
.toString()
.split('\n')
.filter(Boolean)
.filter((f) => ['.js', '.jsx', '.ts', '.tsx'].includes(path.extname(f)));
for (const filePath of files) {
const matches = fs
.readFileSync(filePath, 'utf-8')
.match(/https:\/\/docs\.mongodb\.com([^\b\s\n])+/g);
const urls = (matches || []).map((u) => u.split(/['"`]/)[0]);
links = [...links, ...urls];
}
const uniqueLinks = new Set(links);
let errors = [];
for (const link of uniqueLinks) {
try {
console.log(link);
const resp = await fetch(link);
if (resp.status >= 400) {
errors.push([link, resp.statusText]);
}
const parsedUrl = new URL(link);
const anchor = (parsedUrl.hash || '').replace(/^#/, '');
if (anchor) {
const text = await resp.text();
const dom = new JSDOM(text);
const idElem = dom.window.document.getElementById(anchor);
const aName = dom.window.document.querySelectorAll(
`a[name="${anchor}"]`
);
if (!(idElem || aName[0])) {
errors.push([link, 'anchor not found']);
}
}
} catch (e) {
errors.push([link, e.message]);
}
}
if (errors.length) {
console.log('Found broken links:');
console.log(JSON.stringify(Object.fromEntries(errors), null, 2));
process.exit(1);
}
}
main().catch((e) => {
console.log(e);
process.exit(1);
});