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.
37 lines (30 loc) • 885 B
JavaScript
import fetch from 'node-fetch';
function handleError(error) {
throw new Error(
error.message
);
}
async function checkStatus(linksArray) {
try {
const statusArray = await Promise.all(linksArray.map(async url => {
const res = await fetch(url);
return res.status;
}));
return statusArray;
} catch (error) {
handleError(error);
}
}
function generateArrayURLS(linksArray) {
return linksArray.map(linkObject => Object.values(linkObject).join());
}
async function validateURLS(linksArray) {
const links = generateArrayURLS(linksArray);
const linksStatus = await checkStatus(links);
const results = linksArray.map((object, index) => ({
...object,
status: linksStatus[index]
}));
return results;
}
export default validateURLS;