forked from Laboratoria/CDMX009-MdLinks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (72 loc) · 2.35 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
//Dependencias
const fs = require('fs');
const path = require("path");
const fetch = require('node-fetch');
const chalk = require('chalk');
const log = console.log;
let uri = "";
let content = "";
function getPath() {
let index = process.argv.indexOf("--file")
if (index < 0) { return log(chalk.bgRed("Por favor ingresa el argumento --file seguido de la ruta del archivo Markdown.")) }
let uri = process.argv[index + 1]
let extension = path.extname(uri)
// log(chalk.black.bgYellowBright("extension:", extension))
if (extension != '.md') {
log(chalk.bgRed("Por favor ingresa la ruta de un archivo Markdown (extensión .md)"));
} else {
return uri
}
}
uri = getPath();
function readFile(uri) {
let content = fs.readFileSync(uri, "utf8")
return content
}
// Falta promesa en readfile,
content = readFile(uri)
//log(chalk.bgCyan("text content: ", content));
function searchLinks(content) {
let regEx = (/https?:\S+\w/g);
let links = content.match(regEx)
return links
}
let links = searchLinks(content)
log(chalk.cyan("Founded links :", links));
let totalLinks = links.length;
log(chalk.black.bgCyan("total links:", totalLinks));
let arrayOfBrokenLinks = [];
let arrayOfCorrectLinks = [];
function verifyLinks(links) {
// let indexVal = process.argv.indexOf("--validate")
// if (indexVal < 0) {
links.forEach(link => {
fetch(link)
.then(res => {
let status = res.status
if (status === 200) {
arrayOfCorrectLinks.push(link)
log(chalk.bgGreen(link, "Status OK: ", status))
}
else {
arrayOfBrokenLinks.push(link)
log(chalk.bgRed(link, "Broken link: ", status))
}
}
)
})
}
// }
verifyLinks(links)
log(chalk.cyan("Total links:", totalLinks));
log(chalk.bgGreen("Working links: ", arrayOfCorrectLinks));
log(chalk.bgRedBright("Broken links: ", arrayOfBrokenLinks));
// log("status: ", verifyLinks(links))
// function statistics(links) {
// let total = links.lenght
// log(chalk.cyan("Total links:", total));
// log(chalk.bgGreen("Working links: ", arrayOfCorrectLinks.lenght));
// log(chalk.bgRedBright("Broken links: ", arrayOfBrokenLinks.lenght));
// }
// statistics(links)