-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlint-markdown.js
67 lines (61 loc) · 2.11 KB
/
lint-markdown.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
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable @typescript-eslint/no-var-requires */
const markdownLinkCheck = require("markdown-link-check");
const chalk = require("chalk");
const path = require("path");
const fs = require("fs");
const statusLabels = {
alive: chalk.green("✓"),
dead: chalk.red("✖"),
ignored: chalk.gray("/"),
error: chalk.yellow("⚠"),
};
const options = {
// ignorePatterns: [{ pattern: "^http(s)://" }],
retryOn429: true,
retryCount: 2,
fallbackRetryDelay: "30s",
aliveStatusCodes: [200, 206],
};
function getFiles(dir, extension, result = []) {
const dirents = fs.readdirSync(dir, { withFileTypes: true });
for (const dirent of dirents) {
const res = path.resolve(dir, dirent.name);
if (dirent.isDirectory()) getFiles(res, extension, result);
else if (res.endsWith(extension)) result.push(res);
}
return result;
}
function checkFile(file) {
return new Promise((resolve, reject) => {
const opts = {
...options,
baseUrl: `file://${path.dirname(file)}`,
};
markdownLinkCheck(fs.readFileSync(file, "utf-8"), opts, (err, results) => {
const relativeFile = path.relative(process.cwd(), file);
if (err) {
console.log(relativeFile, err);
reject(err);
} else if (results.length) {
const failed = results.filter((result) => result.status !== "alive" && result.status !== "ignored");
console.log(relativeFile);
if (failed.length) {
for (const result of failed) {
console.log("- [%s] %s", statusLabels[result.status], result.link);
}
reject();
} else {
resolve();
}
}
});
});
}
async function checkAllFiles() {
const results = await Promise.allSettled(getFiles("docs", ".md").map(checkFile));
if (results.some(({ status }) => status !== "fulfilled")) {
process.exit(-1);
}
}
checkAllFiles();