UNPKG

markdown-url-checker

Version:

The lib takes care of checking every link inside a markdown file to see if they're all up.

28 lines (24 loc) 800 B
const chalk = require('chalk'); const fs = require('fs'); function extraiLinks(texto) { const regex = /\[([^\]]*)\]\((https?:\/\/[^$#\s].[^\s]*)\)/gm; const arrayResultados = []; let temp; while((temp = regex.exec(texto)) !== null) { arrayResultados.push({ [temp[1]]: temp[2] }) } return arrayResultados.length === 0 ? 'não há links' : arrayResultados; } function trataErro(erro) { throw new Error(chalk.red(erro.code, 'Arquivo não encontrado no caminho especificado')); } async function pegaArquivo(caminhoDoArquivo) { const encoding = 'utf-8'; try { const texto = await fs.promises.readFile(caminhoDoArquivo, encoding); return extraiLinks(texto); } catch(erro) { trataErro(erro) } } module.exports = pegaArquivo;