remark-validate-links
Advanced tools
Comparing version 9.1.0 to 9.2.0
'use strict' | ||
var fs = require('fs') | ||
var path = require('path') | ||
@@ -24,3 +25,7 @@ var URL = require('url').URL // Node 8 support | ||
function find(ctx) { | ||
// List from: https://github.com/github/markup#markups | ||
var readmeExtensions = ['.markdown', '.mdown', '.mkdn', '.md'] | ||
var readmeBasename = /^readme$/i | ||
function find(ctx, next) { | ||
var file = ctx.file | ||
@@ -32,3 +37,6 @@ var fileSet = ctx.fileSet | ||
var landmarks = {} | ||
var alreadyAdded = [] | ||
var actual = 0 | ||
var expected = 0 | ||
var statted = [] | ||
var added = [] | ||
var config = xtend(ctx.urlConfig, { | ||
@@ -43,3 +51,3 @@ path: absolute, | ||
landmarks[absolute] = {'': true} | ||
addLandmarks(absolute, '') | ||
@@ -50,2 +58,6 @@ slugs.reset() | ||
if (expected === 0) { | ||
next() | ||
} | ||
function mark(node) { | ||
@@ -56,3 +68,2 @@ var data = node.data || {} | ||
var info = node.url ? urlToPath(node.url, config) : null | ||
var refs | ||
var fp | ||
@@ -66,3 +77,3 @@ var hash | ||
if (id) { | ||
landmarks[absolute][id] = true | ||
addLandmarks(absolute, id) | ||
} | ||
@@ -73,26 +84,106 @@ | ||
hash = info.hash | ||
refs = references[fp] || (references[fp] = {}) | ||
add('', node) | ||
addReference(fp, '', node) | ||
// With a heading | ||
if (hash) { | ||
if (fileSet || fp === absolute) { | ||
add(hash, node) | ||
addReference(fp, hash, node) | ||
} | ||
if (fileSet && fp && alreadyAdded.indexOf(fp) === -1) { | ||
alreadyAdded.push(fp) | ||
fileSet.add(vfile({cwd: file.cwd, path: path.relative(file.cwd, fp)})) | ||
if (fileSet && fp && statted.indexOf(fp) === -1) { | ||
addFile(fp) | ||
} | ||
} | ||
} | ||
} | ||
function add(hash, node) { | ||
if (refs[hash]) { | ||
refs[hash].push(node) | ||
function addLandmarks(filePath, hash) { | ||
addLandmark(filePath, hash) | ||
// Note: this may add marks too many anchors as defined. | ||
// For example, if there is both a `readme.md` and a `readme.markdown` in a | ||
// folder, both their landmarks will be defined for their parent folder. | ||
// To solve this, we could check whichever sorts first, and ignore the | ||
// others. | ||
// This is an unlikely scenario though, and adds a lot of complexity, so | ||
// we’re ignoring it. | ||
if (readme(filePath)) { | ||
addLandmark(path.dirname(filePath), hash) | ||
} | ||
} | ||
function addLandmark(filePath, hash) { | ||
var marks = landmarks[filePath] || (landmarks[filePath] = {}) | ||
marks[hash] = true | ||
} | ||
function addReference(filePath, hash, node) { | ||
var refs = references[filePath] || (references[filePath] = {}) | ||
var hashes = refs[hash] || (refs[hash] = []) | ||
hashes.push(node) | ||
} | ||
function addFile(fileOrFolderPath) { | ||
expected++ | ||
statted.push(fileOrFolderPath) | ||
fs.stat(fileOrFolderPath, onstat) | ||
function onstat(_, stats) { | ||
if (stats && stats.isDirectory()) { | ||
fs.readdir(fileOrFolderPath, onreaddir) | ||
} else { | ||
refs[hash] = [node] | ||
done(fileOrFolderPath) | ||
} | ||
} | ||
function onreaddir(_, entries) { | ||
/* istanbul ignore next - unlikely that it is an unreadable directory. */ | ||
var files = (entries || []).sort() | ||
var length = files.length | ||
var index = -1 | ||
var entry | ||
var file | ||
var filePath | ||
while (++index < length) { | ||
entry = entries[index] | ||
if (readme(entry)) { | ||
file = entry | ||
break | ||
} | ||
} | ||
// If there is no readme that we can parse, add the directory. | ||
filePath = fileOrFolderPath | ||
// To do: test for no readme in directory. | ||
if (file) { | ||
filePath = path.join(fileOrFolderPath, file) | ||
statted.push(filePath) | ||
} | ||
done(filePath) | ||
} | ||
function done(filePath) { | ||
if (added.indexOf(filePath) === -1) { | ||
added.push(filePath) | ||
fileSet.add( | ||
vfile({cwd: file.cwd, path: path.relative(file.cwd, filePath)}) | ||
) | ||
} | ||
actual++ | ||
if (actual === expected) { | ||
next() | ||
} | ||
} | ||
} | ||
@@ -207,1 +298,10 @@ } | ||
} | ||
function readme(filePath) { | ||
var ext = path.extname(filePath) | ||
return ( | ||
readmeExtensions.indexOf(ext) !== -1 && | ||
readmeBasename.test(path.basename(filePath, ext)) | ||
) | ||
} |
{ | ||
"name": "remark-validate-links", | ||
"version": "9.1.0", | ||
"version": "9.2.0", | ||
"description": "remark plugin to validate links to headings and files", | ||
@@ -20,2 +20,6 @@ "license": "MIT", | ||
"bugs": "https://github.com/remarkjs/remark-validate-links/issues", | ||
"funding": { | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/unified" | ||
}, | ||
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)", | ||
@@ -50,3 +54,2 @@ "contributors": [ | ||
"devDependencies": { | ||
"execa": "^4.0.0", | ||
"nyc": "^15.0.0", | ||
@@ -53,0 +56,0 @@ "prettier": "^1.0.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28600
10
595
3