forked from Laboratoria/bog001-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·71 lines (51 loc) · 1.71 KB
/
index.js
File metadata and controls
executable file
·71 lines (51 loc) · 1.71 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
// llamando los modulos
const fs = require('fs');
const fsPromises = require('fs').promises
let Promise = require("bluebird");
const path = require('path');
const marked = require('marked');
const jsdom = require("jsdom");
const { leerDirectorio } = require('./funciones');
const { JSDOM } = jsdom;
//esta funcion retorna un array de objetos con los datos que necesito
const objetoLinks = (data, rutaOriginal) => {
const html = marked(data)
let dom = new JSDOM(html)
let arrayLinks = [];
const document = dom.window.document;
let linksHTML = document.querySelectorAll("a");
Object.values(linksHTML).forEach(archivo => {//necesito el object.values?
let parentesisKill = /[()"]+/g;
let resultLink = archivo.href.replace(parentesisKill, '');
arrayLinks.push({text: archivo.text, href: resultLink, file: rutaOriginal,});
})
return arrayLinks
}
// mi funcion principal con mis rutas
const mdLinks = (archivoPath) => {
//const pruebaRuta = path.resolve(archivoPath)
const rutasArray = leerDirectorio(archivoPath);
const arrayObjetosLinks = rutasArray.map((ruta) =>{
rutaAbsolutaDeMisArchivos = path.resolve(ruta)
console.log(rutaAbsolutaDeMisArchivos);
return fsPromises.readFile(rutaAbsolutaDeMisArchivos, 'utf8',)
.then(function(result) {
let linksObjeto = objetoLinks(result,rutaAbsolutaDeMisArchivos);
return linksObjeto
})
.catch(function(error) {
console.log(error.message);
})
})
return Promise.all(arrayObjetosLinks).then(results => {
return results.flat()
})
};
/*mdLinks('README.md').then((result) =>{
return validatelink(result);
})
.then((result) => {
console.log(result)
return stats(result);//le mando mi funcion de stats
})*/
module.exports = mdLinks