UNPKG

markdown-links-check

Version:

This lib extract the links in a markdown file, has two options: an array result with all links in the file with the title and the URL or test all the URLs and return the status code for each one.

32 lines (26 loc) 754 B
import chalk from "chalk"; import fs from "fs"; function handleError(error) { throw new Error( chalk.red(error.code, "The specified file could not be accessed.") ); } async function readFile(filepath) { const encoding = "utf-8"; try { const text = await fs.promises.readFile(filepath, encoding); return extractLinks(text); } catch (error) { handleError(error); } } function extractLinks(text) { const regex = /\[([^\]]*)\]\((https?:\/\/[^$#\s].[^\s]*)\)/gm; const resultArray = []; let temp; while((temp = regex.exec(text)) != null) { resultArray.push({ [temp[1]] : temp[2] }) } return resultArray.length === 0 ? 'No links found' : resultArray; } export default readFile;