Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mdls 43 #14

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
65 changes: 65 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

const fn = require ('./fn.js');
const chalk = require ('chalk');

const mdLinks = (filePath, opt) =>
new Promise((resolve, reject) => {

// convertir la ruta en absoluta
const isPathAbsolute = fn.isPathAbsolute(filePath)
? filePath
: fn.convertPathToAbsolute(filePath);



let allFiles = [];
// leer carpetas
if(fn.isFolder(isPathAbsolute)){
allFiles = [...allFiles,...fn.readFolders(isPathAbsolute)];
}else{
allFiles.push(isPathAbsolute) ;

}


// obtener archivos .md
const filesMd = allFiles.filter(file => {
if(fn.getMdFile(file) == '.md'){
return file;
}
})


const linksArray = [];

filesMd.forEach((file) => {
// leer archivos
const filteredFiles = fn.readFile(file)
// obtener links
const filterMethod = /\[(.+)\]\((https?:\/\/.+)\)/gi;
const resultLinks = [...filteredFiles.matchAll(filterMethod)];
if(resultLinks !== null || resultLinks !== 0){
resultLinks.forEach(url => {
linksArray.push({
href: url[2],
text: url[1].slice(0, 50),
file: file
})
})
}

})




resolve(linksArray)

reject('nel no hay nada')


});

module.exports = {
mdLinks
}
24 changes: 24 additions & 0 deletions chalkP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
const chalk = require('chalk');

// prueba de chalk
const log = console.log
log(chalk.blue('Hello') + ' World' + chalk.red('!'));
log(chalk.blue.bgRed.bold('Hello world!'));
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));
log(chalk.green(
'I am a green line ' +
chalk.blue.underline.bold('with a blue substring') +
' that becomes green again!'
));
log(`

${chalk.underline.bgCyanBright('CPU')} : ${chalk.red('90%')}
RAM: ${chalk.green('40%')}
DISK: ${chalk.yellow('70%')}
`);

log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
log(chalk.hex('#DEADED').bold('Bold gray!'));
*/
49 changes: 49 additions & 0 deletions fn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const fs = require ('fs');
const path = require('path');
const axios = require('axios');
const http = require('http');



// Saber si la ruta es absoluta
const isPathAbsolute = (route) => path.isAbsolute(route);

// Convertir la ruta en absoluta
const convertPathToAbsolute = (route) => path.resolve(route);

// Es una carpeta?
const isFolder = (route) => fs.lstatSync(route).isDirectory()

// Leer las carpetas funcion recursiva
const readFolders = (routePath, resultFiles = []) => {
const files = fs.readdirSync(routePath);
files.forEach((file) => {
const newPath = path.join(routePath, file);
if (fs.lstatSync(newPath).isDirectory()){
readFolders(newPath, resultFiles);
} else{
resultFiles.push(newPath)
};
})
return resultFiles;
}

// Leer archivo
const readFile = (route) => fs.readFileSync(route, "utf-8");

// Obtener archivos .md
const getMdFile = (file) => path.extname(file);

// validar links
const validateLinks = (href) => axios.get(href);


module.exports = {
isPathAbsolute,
convertPathToAbsolute,
isFolder,
readFolders,
readFile,
getMdFile,
validateLinks
}
38 changes: 35 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
module.exports = () => {
// ...
};
const fn = require ('./fn.js');
const api = require ('./api');
const axios = require ('axios');
const chalk = require ('chalk');



// not absolute ./README.md
// absolute /Users/jazmin/Downloads/Laboratoria/Mex013/MD_links_S/CDMX013-md-links-S/README.md

// saber si la ruta es absoluta
// console.log(fn.isPathAbsolute('/Users/jazmin/Downloads/Laboratoria/Mex013/MD_links_S/CDMX013-md-links-S/README.md'));

// convertir ruta a absoluta
//console.log(fn.convertPathToAbsolute('./README.md'));

// es una carpeta?
//console.log(fn.isFolder('test-files'));

// leer las carpetas
//console.log(fn.readFolders('test-files'))

// leer archivo
// console.log(fn.readFile("text.txt"));

// obtener archivo .md
// console.log(fn.getMdFile("README.")) // luego obterner solo los archivos .md

// validar links



api.mdLinks(process.argv[2])
.then(result => console.log(result))
.catch(result => console.log(result));
Loading