forked from Laboratoria/SCL013-md-links
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
150 lines (137 loc) · 4.64 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* eslint-disable max-len */
const fs = require('fs');
const md = require('markdown-it')();
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const chalk = require('chalk');
const nodeFetch = require('node-fetch');
const figlet = require('figlet');
figlet('Md Links !', {
font: 'isometric1',
horizontalLayout: 'default',
verticalLayout: 'default',
}, (err, result) => {
// eslint-disable-next-line no-console
console.log(chalk.magenta(err || result));
});
// Función para limitar texto a 50 caracteres
const limitText = (text) => {
if (text.length > 50) {
const textFile = text.slice(0, 50);
return textFile;
} return text;
};
// Función para validar el estatus de la url
const validateUrl = (links) => {
// eslint-disable-next-line array-callback-return
links.map((link) => {
nodeFetch(link.href)
.then((resp) => {
if (resp.statusText === 'OK') {
// eslint-disable-next-line no-console
console.log(chalk.greenBright('Link WORKING', chalk.magenta('status:'), `${resp.statusText}`, `${resp.status}`, chalk.magenta('href:'), `${link.href}`, chalk.magenta('text:'), `${link.text}`, chalk.magenta('file:'), `${link.file}`));
} else if (resp.status === 404) {
// eslint-disable-next-line no-console
console.log(chalk.redBright('Link BROKEN', chalk.magenta('status:'), 'FAIL', `${resp.status}`, chalk.magenta('href:'), `${link.href}`, chalk.magenta('text:'), `${link.text}`, chalk.magenta('file:'), `${link.file}`));
}
})
.catch((error) => {
// eslint-disable-next-line no-console
console.log(chalk.yellow('Link con errores', chalk.magenta('href:'), `${link.href}`, chalk.magenta('error:'), `${error}`));
});
});
};
// Función que calcula total de links y links unicos
const printTotalLinks = (links) => {
const numOfLinks = [];
links.forEach((link) => {
numOfLinks.push(link.href);
});
const uniqueLinks = new Set(numOfLinks);
// eslint-disable-next-line no-console
console.log(
chalk.black.bgGreen('Total: '),
chalk.green(numOfLinks.length),
chalk.black.bgYellow('Unique: '),
chalk.yellow(uniqueLinks.size),
);
};
// Función que calcula total de links rotos
const printTotalBroken = (links) => {
const linksHref = links.map((link) => link.href);
let brokenLinks;
let countBroken = 0;
linksHref.forEach((elem) => {
brokenLinks = nodeFetch(elem)
.then((resp) => {
if (resp.status !== 200) {
countBroken += 1;
}
return countBroken;
})
.catch((error) => {
// eslint-disable-next-line no-console
console.log(error);
});
});
brokenLinks.then((res) => {
// eslint-disable-next-line no-console
console.log(chalk.black.bgRed('Broken: '), chalk.redBright(res));
});
};
// Función para verificar el archivo .md y extraer los anchors devolviendo un array de objetos
const verifyMdFile = (dom, path, options) => {
const links = dom.window.document.querySelectorAll('a');
const linksArray = Array.from(links);
const filteredLinks = linksArray.filter((a) => a.href.includes('http'));
// eslint-disable-next-line arrow-body-style
const objects = filteredLinks.map((a) => {
return {
text: limitText(a.innerHTML),
href: a.href,
file: path,
};
});
if (options.validate === true && options.stats === true) {
validateUrl(objects);
printTotalLinks(objects);
printTotalBroken(objects);
} else if (options.validate === true) {
validateUrl(objects);
} else if (options.stats === true) {
printTotalLinks(objects);
printTotalBroken(objects);
} else {
objects.forEach((link) => {
// eslint-disable-next-line no-console
console.log(chalk.blueBright('=>'), chalk.magentaBright(link.href), chalk.cyanBright.bold(link.text), chalk.bgBlue.bold(link.file));
});
}
};
// Función que lee el archivo .md
// eslint-disable-next-line arrow-body-style
const readingFile = (path, options = { validate: false, stats: false }) => {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
})
.then((data) => {
// eslint-disable-next-line no-console
console.log(chalk.bgMagenta('Lectura de Archivo: ', path));
const html = md.render(data.toString());
const dom = new JSDOM(html);
verifyMdFile(dom, path, options);
})
.catch(() => {
// eslint-disable-next-line no-console
console.log(chalk.yellow(('Por Favor ingresa un archivo de extensión .md')));
});
};
module.exports = {
limitText, validateUrl, readingFile, verifyMdFile,
};