forked from Laboratoria/DEV007-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
52 lines (47 loc) · 1.3 KB
/
utils.js
File metadata and controls
52 lines (47 loc) · 1.3 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
const fetch = require("node-fetch");
function extractLinks(fileContent) {
const searchingLinks = /\[(.+)\]\(([^ ]+?)( "(.+)")?\)/g
const linksFound = [];
let match;
while((match = searchingLinks.exec(fileContent)) !== null) {
const text = match[1];
const url = match[2];
linksFound.push({text,url});
};
return linksFound;
}
const validateLink = (link) => {
if (link && link.url){
return fetch(link.url)
.then((response) =>{
if(response.status >= 200 && response.status < 400) {
link.status = response.status;
link.ok = "Ok";
return link;
} else {
link.status = response.status;
link.ok = "Broke";
return link;
}
})
.catch((error) => {
link.status = "Error";
link.ok = "Failed";
return link;
})
}}
const stats =(links) => {
return {
Total: links.length,
Unique: new Set(links.map((link) => link.url)).size,
}
};
const statsAndValidate = (links) => {
const brokenLinks = links.filter((link) => link.ok !=="Ok").length;
return {
Total: links.length,
Unique: new Set(links.map((link) => link.url)).size,
Broken: brokenLinks,
}
}
module.exports = { extractLinks, validateLink, stats, statsAndValidate };