Skip to content

Commit d49c30e

Browse files
committed
Se agrega funcion validateLinks a la funcion mdLinks
1 parent 68aae0d commit d49c30e

File tree

6 files changed

+56
-52
lines changed

6 files changed

+56
-52
lines changed
File renamed without changes.

test/files-md/prueba.md files-md/prueba.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
ligero muy popular entre developers. Es usado en muchísimas plataformas que
55
manejan texto plano (GitHub, foros, blogs, ...) y es muy común
66
encontrar varios archivos en ese formato en cualquier tipo de repositorio
7-
(empezando por el tradicional `README.md`).
7+
(empezando por el tradicional `README.md`).
File renamed without changes.

index.js src/index.js

+34-16
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,47 @@ const {
77
readFile,
88
createPathFile,
99
readLinks,
10+
validateLink,
1011
} = require("./main.js");
1112

12-
function mdLinks(path) {
13+
const mdLinks = (path, options = {}) => {
1314
// Manejo de ruta relativa
1415
const pathIsAbsolute = fnIsAbsolute(path);
1516

1617
if (!pathIsAbsolute) {
1718
const absoluteVersion = fnConvertToAbsolute(path);
1819
console.log("se covierte a ruta absoluta: " + absoluteVersion);
19-
return mdLinks(absoluteVersion);
20+
return mdLinks(absoluteVersion, options);
2021
}
2122

2223
// Verificación de existencia de la ruta
2324
const pathExists = verifyRoute(path);
2425
console.log("La ruta existe: " + pathExists);
2526

2627
if (!pathExists) {
27-
console.error("El path ingresado es inválido");
28-
return;
28+
return new Promise((resolve, reject) => {
29+
reject("Rejected: El path ingresado es inválido");
30+
});
2931
}
3032

3133
const pathType = veriFyIsFileOrDirectory(path);
3234
console.log("La ruta es de tipo: " + pathType);
3335

34-
readFileOrDirectory(path, pathType)
35-
.then((links) => {
36-
console.log(links);
37-
})
38-
.catch((error) => {
39-
console.log("Error al obtener los archivos:", error);
40-
});
41-
}
36+
return readFileOrDirectory(path, pathType).then((links) => {
37+
if (options.validate) {
38+
const arrPromesas = links.map((link) => {
39+
// validar el link (status y ok)
40+
return validateLink(link.href).then((validation) => {
41+
// retornara un objeto con las 5 propiedades ( text, href, file, status, ok)
42+
return { ...link, ...validation };
43+
});
44+
});
45+
return Promise.all(arrPromesas);
46+
} else {
47+
return links;
48+
}
49+
});
50+
};
4251

4352
function readFileOrDirectory(path, pathtype) {
4453
if (pathtype === "directory") {
@@ -63,7 +72,9 @@ function readFileOrDirectory(path, pathtype) {
6372
});
6473

6574
//Se retorna todas las promesas
66-
return Promise.all(promisesFilesLinks);
75+
return Promise.all(promisesFilesLinks).then((arrLinks) =>
76+
arrLinks.flat()
77+
);
6778
});
6879
} else if (pathtype === "file") {
6980
if (!path.endsWith(".md")) {
@@ -84,6 +95,13 @@ function readFileOrDirectory(path, pathtype) {
8495
}
8596
}
8697

87-
mdLinks(
88-
"C:/Users/diana/Documents/Projects/Laboratoria/md-links/test/files-md/"
89-
);
98+
//---------------------------------------------------------------------------------
99+
100+
mdLinks("C:/Users/diana/Documents/Projects/Laboratoria/md-links/files-md", {
101+
validate: true,
102+
})
103+
.then((links) => {
104+
console.log(links);
105+
// => [{ href, text, file, status, ok }, ...]
106+
})
107+
.catch((e) => console.error(e));

main.js src/main.js

+17-28
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ function readDirectory(directoryRoute) {
6060
return readdir(directoryRoute);
6161
}
6262

63+
//Leer archivos
64+
// retorna el contenido - data del archivo
65+
function readFile(filePath) {
66+
return new Promise((resolve, reject) => {
67+
fs.readFile(filePath, "utf8", (error, data) => {
68+
if (error) return reject(error);
69+
resolve(data);
70+
});
71+
});
72+
}
73+
6374
function readLinks(data) {
6475
// [Acerca de Node.js - Documentación oficial](https://nodejs.org/es/about/)
6576

@@ -75,40 +86,15 @@ function readLinks(data) {
7586
return links;
7687
}
7788

78-
//Leer archivos
79-
// retorna el contenido - data del archivo
80-
function readFile(filePath) {
81-
return new Promise((resolve, reject) => {
82-
fs.readFile(filePath, "utf8", (error, data) => {
83-
if (error) return reject(error);
84-
resolve(data);
85-
});
86-
});
87-
}
88-
89-
// Este codigo va en md-liks
90-
// readFile(
91-
// "C:/Users/diana/Documents/Projects/Laboratoria/md-links/test/files-md/prueba2.md"
92-
// )
93-
// .then((data) => {
94-
// // console.log(data)
95-
// readLinks(data);
96-
// })
97-
// .catch((error) => console.log("Este archivo no se puede leer", error));
98-
9989
// creacion de ruta con el nombre de archivo iterado
10090
// retorna la ruta del archivo
10191

10292
function createPathFile(route, fileName) {
10393
return (pathFile = path.join(route, fileName));
10494
}
10595

106-
// createPathFile("C:/Users/diana/Documents/Projects/Laboratoria/md-links/files-md","/prueba2.md");
107-
// console.log(veriFyIsFileOrDirectory ("C:/Users/diana/Documents/Projects/Laboratoria/md-links/README.md"));
108-
// console.log( readDirectory("C:/Users/diana/Documents/Projects/Laboratoria/md-links"));
109-
110-
function validate(url) {
111-
return fetch(url)
96+
function validateLink(href) {
97+
return fetch(href)
11298
.then((response) => {
11399
const status = response.status;
114100
const ok = response.ok ? "ok" : "fail";
@@ -119,7 +105,9 @@ function validate(url) {
119105
});
120106
}
121107

122-
validate("https://api.nijrex.pe/asdasd");
108+
// createPathFile("C:/Users/diana/Documents/Projects/Laboratoria/md-links/files-md","/prueba2.md");
109+
// console.log(veriFyIsFileOrDirectory ("C:/Users/diana/Documents/Projects/Laboratoria/md-links/README.md"));
110+
// console.log( readDirectory("C:/Users/diana/Documents/Projects/Laboratoria/md-links"));
123111

124112
module.exports = {
125113
fnIsAbsolute,
@@ -130,4 +118,5 @@ module.exports = {
130118
readFile,
131119
readLinks,
132120
createPathFile,
121+
validateLink,
133122
};

test/md-links.spec.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
const mdLinks = require('../');
1+
const mdLinks = require("../src");
22

3-
4-
describe('mdLinks', () => {
5-
6-
it('should...', () => {
7-
console.log('FIX ME!');
3+
describe("mdLinks", () => {
4+
it("should...", () => {
5+
console.log("FIX ME!");
86
});
9-
107
});

0 commit comments

Comments
 (0)