Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"ibm.output-colorizer"
]
}
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"workbench.preferredHighContrastLightColorTheme": "Default Dark Modern",
"workbench.preferredLightColorTheme": "Visual Studio Dark",
"editor.semanticTokenColorCustomizations": {
"[Default Dark Modern]": {}
},
"editor.tokenColorCustomizations": {
"[Default Dark Modern]": {}
}
}
1,293 changes: 1,293 additions & 0 deletions Files/Untitled-1.jsonc

Large diffs are not rendered by default.

Empty file added Files/readme.md
Empty file.
689 changes: 100 additions & 589 deletions README.md

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env node

const { mdLinks, statsValidatelinks } = require("./index.js");
const colors = require("colors");

const command = process.argv[2];
const validateOption = process.argv.includes("--validate");
const statsOption = process.argv.includes("--stats");

const options = { validate: validateOption, stats: statsOption };

if (command) {
mdLinks(command, options)
.then((result) => {
if (result.length > 0) {
if (options.stats && options.validate) {
result.flat().forEach((link) => {
const statusColor = link.ok === "ok" ? colors.green : colors.red;
console.log(`(${colors.cyan(link.file)}, ${colors.gray(link.href)}, ${colors.blue(link.text)}, ${statusColor(link.ok)}, ${colors.red(link.status)})`);
});
const stats = statsValidatelinks(result.flat());
console.log(colors.blue(`Total: ${stats.Total}, Unique: ${stats.Unique}, Broken: ${stats.Broken}`));
} else if (options.stats) {
const stats = statsValidatelinks(result.flat());
console.log(colors.blue(`Total: ${stats.Total}, Unique: ${stats.Unique}, Broken: ${stats.Broken}`));
} else if (options.validate) {
result.flat().forEach((link) => {
const statusColor = link.ok === "ok" ? colors.green : colors.red;
console.log(`(${colors.cyan(link.file)}, ${colors.gray(link.href)}, ${colors.blue(link.text)}, ${statusColor(link.ok)}, ${colors.red(link.status)})`);
});
} else {
result.flat().forEach((link) => {
console.log(`(${colors.blue(link.text)}, (${colors.cyan(link.file)}, ${colors.gray(link.href)})`);
});
}
} else {
console.log(colors.red.bold("No links were found in the file."));
}
})
.catch((error) => {
console.error(error);
});
}

Binary file added img/Banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgReadme/Flowchart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgReadme/Img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgReadme/Planning.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
223 changes: 221 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,222 @@
module.exports = () => {
// ...
const { ExtractLinks } = require("markdown-link-extractor");
const fs = require("fs"); // para los archivos
const path = require("path"); // para las rutas
const colors = require("colors"); // para el estilo (color)
const axios = require("axios"); // para hacer solicitudes HTTP

// Mdlink Function
const mdLinks = (inputPath = process.argv[2], options = { validate: false, stats: false }) => {
return new Promise((resolve, reject) => {
// DF 1
// Check if the path valid.
if (!fs.existsSync(inputPath)) {
reject(colors.brightRed.bold("The path doesn't exist"));
return;
}
// DF 2
// Check if a path is absolute
if (!path.isAbsolute(inputPath)) {
// DF 3
// Convert the path to an absolute path
inputPath = path.resolve(inputPath);
}
// DF 4
// Check if it is a directory and read it
const isDirectory = fs.statSync(inputPath).isDirectory();
if (isDirectory === true) {
// aqui exploro el directorio
const getMdFiles = (dirPath) => {
// DF 5 Read a directory (recursivity)
const files = fs.readdirSync(dirPath); // aqui tengo lista de archivos/subdirectorios(S/A)
const promises = [];
files.forEach((file) => {
// recorro esa lista
const filePath = path.join(dirPath, file); // aqui defino la ruta de cada archivo
const isSubDirectory = fs.statSync(filePath).isDirectory();
if (isSubDirectory) {
promises.push(getMdFiles(filePath)); // aqui entra mi exploracion de sub directorios (recursividad)
// DF 6 Does is a md file?
} else if (path.extname(filePath) === ".md") {
// DF 7 Read MD file. CALL readMarkdownFile function: continue below
promises.push(readMarkdownFile(filePath, options));
}
});
return Promise.all(promises);
};

//
getMdFiles(inputPath)
.then((links) => {
const allLinks = links.flat();
if (options.validate) {
const linkPromises = allLinks.map((link) => validateLink(link));
return Promise.all(linkPromises);
} else {
return allLinks;
}
})
.then((result) => {
if (options.stats && options.validate) {
printStatsAndResolve(result, resolve);
} else if (options.stats) {
printStats(result, resolve);
} else if (options.validate) {
resolve(result);
} else {
resolve(result);
}
})
.catch((error) => {
reject(error);
});
} else if (path.extname(inputPath) === ".md") {
readMarkdownFile(inputPath, options)
.then((links) => {
if (options.stats && options.validate) {
printStatsWithBroken(links);
} else if (options.stats && !options.validate) {
printStats(links);
} else if (options.validate) {
resolve(links);
} else {
resolve(links);
}
})
.catch((error) => {
reject(error);
});
} else {
reject("The path does not exist .md file");
}
});
};
// DF 7 cont. Read md.links
const readMarkdownFile = (filePath, options) => {
return new Promise((resolve, reject) => {
//console.log(colors.green.underline(filePath));
const fileContent = fs.readFileSync(filePath, "utf-8");
const regex = /\[([^\]]+)\]\(([^\)]+)\)/g; // declaro una expresion regular para los Links MD
const links = []; // donde quiero almacenar
let match;
// DF 8 . Has a URL?
while ((match = regex.exec(fileContent)) !== null) {
// ciclo de busqueda de links con el metodo .exec
const text = match[1];
const href = match[2];
links.push({ text, href, file: filePath }); // pucheo cada link en mi arreglo
}
// console.log(links);
//DF 9. Extrac the links
const filteredLinks = links.filter((link) => !/\d+/.test(link.text)); // dejo o filtro los enlaces quitando los que tienen números
// console.log(filteredLinks);
if (options.validate) { // Validate
const linkPromises = filteredLinks.map((link) => validateLink(link));
Promise.all(linkPromises)
.then((validatedLinks) => {
if (options.stats) { // Stats
resolve(validatedLinks);
printStatsWithBroken(validatedLinks);
} else {
resolve(validatedLinks);
}
})
.catch((error) => {
reject(error);
});
} else if (options.stats) { // Stats
printStats(filteredLinks);
} else {
resolve(filteredLinks);
}
});
};
// Helper function to print stats and resolve the validated links
const printStatsAndResolve = (links, resolve) => {
resolve(links);
printStatsWithBroken(links);
};
const printStatsWithBroken = (links) => {
const stats = statsValidatelinks(links);
console.log(colors.blue(`Total: ${stats.Total}, Unique: ${stats.Unique}, Broken: ${stats.Broken}`));
};
// DF 10.1 Validate links
const validateLink = (link) => {
return new Promise((resolve, reject) => {
axios
.get(link.href)
.then((response) => {
link.status = response.status;
link.ok = response.statusText === "OK" ? "ok" : "fail";
resolve(link);
})
.catch((error) => {
link.status = "Unknown";
link.ok = "fail";
resolve(link);
});
});
};

// DF 10.2 PrintStats Functions
const printStats = (links) => {
const stats = statsLinks(links);
console.log(colors.blue(`Total: ${stats.Total}, Unique: ${stats.Unique}`));
};

// DF 9 StatsLinks Function
const statsLinks = (links) => {
return {
Total: links.length,
Unique: new Set(links.map((link) => link.href)).size,
};
};

// DF 10.3 StatsLinks & ValidateLinks Function
const statsValidatelinks = (links) => {
const fails = links.filter(link => link.ok === 'fail').length;
return {
Total: links.length,
Unique: new Set(links.map((link) => link.href)).size,
Broken: fails
};
};

tsWithBroken = (links) => {
const stats = statsValidatelinks(links);
return stats;
//console.log(`Total: ${stats.Total}`);
//console.log(`Unique: ${stats.Unique}`);
//console.log(`Broken: ${stats.Broken}`);
};
module.exports = {
mdLinks,
statsLinks,
statsValidatelinks,
readMarkdownFile,
validateLink,
printStats,
printStatsWithBroken,
};























Loading