forked from Laboratoria/BOG005-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
148 lines (133 loc) · 3.97 KB
/
functions.js
File metadata and controls
148 lines (133 loc) · 3.97 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
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
const fs = require('fs');
const pathNode = require('node:path');
const marked = require('marked');
const fetch = require('node-fetch');
// Validar si la ruta es absoluta, sino se convierte en relativa
const getAbsolutePath = (userPath) => {
let pathAbsolute = '';
if(!userPath){
return false
}
if(pathNode.isAbsolute(userPath)){
pathAbsolute = userPath;
}else{
pathAbsolute = pathNode.resolve(userPath)
}
return pathAbsolute
}
// Obtener Archivos de una carpeta
const getFiles = (path) => {
let files = []
if(fs.statSync(path).isFile()){
files.push(path)
}
else if(fs.statSync(path).isDirectory()){
const filesInDir = fs.readdirSync(path)
filesInDir.forEach( file => {
const pathDir = pathNode.join(path, file)
files = files.concat(getFiles(pathDir))
})
}
return files
}
// Filtar archivos md
const getMdFiles = (arrayFiles) => {
const mdFiles = arrayFiles.filter( file => pathNode.extname(file) === '.md')
return mdFiles
}
// Leer Archivos md y obtener links
const readFiles = (file) => {
return new Promise ((resolve, reject)=>{
let links = []
fs.readFile(file, 'utf8', (err, data)=>{
if (err) {
console.log('error', err) ;
reject(err)
} else {
// resolve(data)
let renderer = new marked.Renderer();
renderer.link = function (href, title, text) {
if (href.includes('http')) {
links.push({
file: file,
href: href,
text: text,
});
}
};
marked.marked(data, { renderer: renderer });
resolve(links);
}
})
return Promise.all(links).then((res) => res.flat());
})
}
// Iterar en el array de archivos y retornar promesas
const loopFilesMd = (arrayMd) => {
let arrayPromises = arrayMd.map((file)=>{
return readFiles(file)
})
return Promise.all(arrayPromises).then(res=>res.flat())
}
// Validar Links
const httpLinks = (allLinks) => {
let promisesLinks = allLinks.map( link => {
return fetch(link.href)
.then(res => {
if (res.status >= 200 && res.status < 400){
return ({ href: link.href,
file: link.file,
text: link.text,
status: res.status,
OK: res.statusText })
}
else if( res.status >= 400 && res.status < 600){
return ({ href: link.href,
file: link.file,
text: link.text,
status: res.status,
OK: "fail" })
}
})
})
return Promise.all(promisesLinks)
}
// Encontrar Links Unicos
const getUniqueLinks = (arrayLinks) =>{
const uniqueLinks = arrayLinks.reduce((counter, link)=>{
counter[link.href] = ++counter[link.href] || 0
return counter
}, {})
const linksDuplicates = arrayLinks.filter( (link) =>{
return uniqueLinks[link.href]} )
const linksUniques = arrayLinks.length - linksDuplicates.length
return linksUniques
}
// Estadisticas de funciones
const statsLinks = (links) => {
const total = {
'Total' : links.length,
'Unique' : getUniqueLinks(links)
}
return total
}
// validar links en estadisticas
const validateStatsLinks = (links)=>{
const linksBrokens = links.filter( link => link.OK === 'fail' )
const total = {
'Total' : links.length,
'Unique' : getUniqueLinks(links),
'Broken' : linksBrokens.length
}
return total
}
module.exports = {
getAbsolutePath,
getFiles,
getMdFiles,
loopFilesMd,
httpLinks,
statsLinks,
validateStatsLinks,
getUniqueLinks
}