check-all-markdown
Advanced tools
Comparing version 1.1.4 to 1.2.0
20
index.js
@@ -44,3 +44,7 @@ 'use strict' | ||
exports.checkLink = (dir/*:string*/, link/*:string*/, {httpMethod = 'HEAD'}/*:{httpMethod: 'HEAD'|'GET'}*/ = {}) => { | ||
exports.checkLink = ( | ||
basedir/*:string*/, | ||
fileLinkIsIn/*:string*/, | ||
link/*:string*/, | ||
{httpMethod = 'HEAD'}/*:{httpMethod: 'HEAD'|'GET'}*/ = {}) => { | ||
const linkUrl = url.parse(link) | ||
@@ -55,4 +59,4 @@ | ||
? Promise.reject(new Error(`Broken link ${link}`)) | ||
: res.statusCode === 403 && httpMethod === 'HEAD' | ||
? exports.checkLink(dir, link, {httpMethod: 'GET'}) | ||
: httpMethod === 'HEAD' | ||
? exports.checkLink(basedir, fileLinkIsIn, link, {httpMethod: 'GET'}) | ||
: Promise.reject(new Error(`Could not fetch ${link}. status = ${res.statusCode}, method = ${httpMethod}`))) | ||
@@ -62,9 +66,11 @@ } else if (!linkUrl.protocol && !/^[a-z.-]+@[a-z.-]+:/.test(link)) { | ||
.then(() => { | ||
const normalizedPath = link.startsWith('/') ? link.slice(1) : link | ||
const normalizedPath = link.startsWith('/') | ||
? path.resolve(basedir, link.slice(1)) | ||
: path.resolve(basedir, path.dirname(fileLinkIsIn), link) | ||
return Promise.promisify(fs.stat)(path.join(dir, normalizedPath)) | ||
return Promise.promisify(fs.stat)(normalizedPath) | ||
}) | ||
.then(() => true) | ||
.catch(err => err.code === 'ENOENT' | ||
? Promise.reject(new Error(`File ${link} not found in ${dir}`)) | ||
? Promise.reject(new Error(`File ${link} not found in ${fileLinkIsIn}`)) | ||
: err) | ||
@@ -85,3 +91,3 @@ } else { | ||
return Promise.all(links.map(link => | ||
exports.checkLink(dir, link) | ||
exports.checkLink(dir, file, link) | ||
.then(result => null) | ||
@@ -88,0 +94,0 @@ .catch(err => err && Promise.resolve(`${file}: ${link} is broken`)))) |
{ | ||
"name": "check-all-markdown", | ||
"version": "1.1.4", | ||
"version": "1.2.0", | ||
"description": "Checks all markdown files using markdownlint, and checks for broken links", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
7381
104