-
Notifications
You must be signed in to change notification settings - Fork 20
/
md.test.js
54 lines (43 loc) · 1.59 KB
/
md.test.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
const fs = require("fs");
const path = require("path");
const fetch = require("node-fetch");
// Test Url in a Markdown File
const testUrlInMarkdown = (file) => {
let markdown = fs.readFileSync(file).toString();
let urls = [];
let regExp = /\[([^\]]+)\]\(([^)]+)\)/g;
let result = null;
while (result = regExp.exec(markdown)) urls.push(result[2]);
return [urls.length, async () => {
for (let url of urls) {
if (url.includes("://") && !url.split("://")[0].includes("/")) {
// An External url
let res = await fetch(url);
if (![200, 301, 302].includes(res.status)) throw new Error(`External Link Test: ${url} with bad response code ${res.status}`);
} else {
// A Local File
if (!fs.existsSync(path.join(path.dirname(file), url))) throw new Error(`Internal Link Test: ${url} with not found error`);
}
}
}];
}
// Test All *.md File Recurrsively
const exploreFolder = (folder, exclude=[]) => {
fs.readdirSync(folder).forEach(file => {
if (exclude.includes(file)) return;
let fullPath = path.join(folder, file);
if (fs.lstatSync(fullPath).isDirectory()) {
// Explore Sub Folder
exploreFolder(fullPath, exclude);
} else if (file.toLocaleLowerCase().endsWith(".md")) {
// Test Markdown File
it(`tests url avaliability in ${fullPath}`, async () => {
let [testCount, testFunc] = testUrlInMarkdown(fullPath);
console.info(`Validating: '${fullPath}'`);
jest.setTimeout(testCount * 5000);
return await testFunc();
});
}
});
}
exploreFolder(".", ["node_modules"]);