remark-validate-links
Advanced tools
Comparing version 3.0.0 to 4.0.0-alpha.1
179
index.js
@@ -26,2 +26,3 @@ /** | ||
var slug = require('remark-slug'); | ||
var xtend = require('xtend'); | ||
@@ -35,2 +36,8 @@ /* | ||
/* | ||
* Constants. | ||
*/ | ||
var NS = 'remark-validate-links'; | ||
/** | ||
@@ -115,5 +122,6 @@ * Get the `pathname` of `uri`, if applicable. | ||
* @example | ||
* gatherReferences(new File(), {}); | ||
* gatherReferences(new File(), {type: 'root'}, {}); | ||
* | ||
* @param {File} file - Set of virtual files. | ||
* @param {Node} tree - Syntax tree. | ||
* @param {Object.<string, string>} project - GitHub | ||
@@ -125,12 +133,10 @@ * project, with a `user` and `repo` property | ||
*/ | ||
function gatherReferences(file, project) { | ||
function gatherReferences(file, tree, project) { | ||
var cache = {}; | ||
var filePath = file.filePath(); | ||
var directory = file.directory; | ||
var space = file.namespace('mdast'); | ||
var getDefinition; | ||
var prefix = ''; | ||
var ast = space.tree || /* istanbul ignore next */ space.ast; | ||
getDefinition = definitions(ast); | ||
getDefinition = definitions(tree); | ||
@@ -192,6 +198,13 @@ if (project.user && project.repo) { | ||
} else { | ||
link = urljoin(directory, link) + (uri.hash || ''); | ||
// var a = link; | ||
link = urljoin(directory || './', link); | ||
if (uri.hash) { | ||
// console.error('a: ', a, link, '\n\n'); | ||
link += uri.hash; | ||
} | ||
uri = parse(link); | ||
// console.error(['a', directory, link, a]); | ||
} | ||
} | ||
// console.error(['b', link]); | ||
@@ -262,4 +275,4 @@ /* | ||
visit(ast, 'link', onlink); | ||
visit(ast, 'linkReference', onlink); | ||
visit(tree, 'link', onlink); | ||
visit(tree, 'linkReference', onlink); | ||
@@ -270,54 +283,2 @@ return cache; | ||
/** | ||
* Factory to store all markdown files, and headings. | ||
* | ||
* @example | ||
* var gather = gatherExposedFactory(); | ||
* var file = new File('# foo'); | ||
* | ||
* gather(file).done() // {} | ||
* | ||
* @return {Function} - Gatherer. | ||
*/ | ||
function gatherExposedFactory() { | ||
var cache = {}; | ||
/** | ||
* Find headings in `file`. | ||
* | ||
* @property {Object} cache - Found links. | ||
* @param {File} file - Virtual file. | ||
* @returns {Function} - Itself. | ||
*/ | ||
function gather(file) { | ||
var filePath = file.filePath(); | ||
var space = file.namespace('mdast'); | ||
var ast = space.tree || space.ast; | ||
/* | ||
* Ignore files without AST or filename. | ||
*/ | ||
if (filePath && ast) { | ||
cache[filePath] = true; | ||
visit(ast, function (node) { | ||
var data = node.data || {}; | ||
var attrs = data.htmlAttributes || {}; | ||
var id = attrs.name || attrs.id || data.id; | ||
if (id) { | ||
cache[filePath + '#' + id] = true; | ||
} | ||
}); | ||
} | ||
return gather; | ||
} | ||
gather.cache = cache; | ||
return gather; | ||
} | ||
/** | ||
* Check if `file` references headings or files not in | ||
@@ -333,10 +294,5 @@ * `exposed`. If `project` is given, normalizes GitHub blob | ||
* @param {File} file - Set of virtual files. | ||
* @param {Object.<string, string>} project - GitHub | ||
* project, with a `user` and `repo` property | ||
* (optional). | ||
*/ | ||
function validate(exposed, file, project) { | ||
var space = file.namespace('mdast'); | ||
var ast = space.tree || space.ast; | ||
var references = ast ? gatherReferences(file, project) : {}; | ||
function validate(exposed, file) { | ||
var references = file.namespace(NS).references; | ||
var filePath = file.filePath(); | ||
@@ -356,2 +312,6 @@ var reference; | ||
// if (hash) { | ||
// console.error(['hash', reference, hash]); | ||
// } | ||
/* | ||
@@ -395,46 +355,36 @@ * Check if files without `hash` can be linked to. | ||
/** | ||
* Factory to create a new completer. | ||
* Completer. | ||
* | ||
* @example | ||
* var completer = completerFactory({ | ||
* 'user': 'foo', | ||
* 'repo': 'bar' | ||
* }); | ||
* completer(new FileSet(), console.log) | ||
* | ||
* @param {Object} project - GitHub project, if applicable. | ||
* @return {Function} - `completer`, bound to `project`. | ||
* @property {*} pluginId - Unique ID so completers by | ||
* this plug-in are not added multiple times to a single | ||
* file-set pipeline. | ||
* @param {FileSet} set - Virtual file-set. | ||
* @param {function(err?)} done - Callback. | ||
*/ | ||
function completerFactory(project) { | ||
/** | ||
* Completer. | ||
* | ||
* @example | ||
* completer(new FileSet(), console.log) | ||
* | ||
* @property {*} pluginId - Unique ID so completers by | ||
* this plug-in are not added multiple times to a single | ||
* file-set pipeline. | ||
* @param {FileSet} set - Virtual file-set. | ||
* @param {function(err?)} done - Callback. | ||
*/ | ||
function completer(set, done) { | ||
var gatherExposed = gatherExposedFactory(); | ||
function completer(set, done) { | ||
var exposed = {}; | ||
set.valueOf().forEach(gatherExposed); | ||
set.valueOf().forEach(function (file) { | ||
var landmarks = file.namespace(NS).landmarks; | ||
set.valueOf().forEach(function (file) { | ||
/* istanbul ignore else - stdin */ | ||
if (file.filePath()) { | ||
validate(gatherExposed.cache, file, project); | ||
} | ||
}); | ||
if (landmarks) { | ||
exposed = xtend(exposed, landmarks); | ||
} | ||
}); | ||
done(); | ||
} | ||
set.valueOf().forEach(function (file) { | ||
/* istanbul ignore else - stdin */ | ||
if (file.filePath()) { | ||
validate(exposed, file); | ||
} | ||
}); | ||
completer.pluginId = 'remark-validate-links'; | ||
return completer; | ||
done(); | ||
} | ||
completer.pluginId = 'remark-validate-links'; | ||
/** | ||
@@ -464,3 +414,6 @@ * Factory to create a transformer based on the given | ||
function transformer(ast, file) { | ||
var filePath = file.filePath(); | ||
var space = file.namespace(NS); | ||
var links = []; | ||
var landmarks = {}; | ||
var references; | ||
@@ -472,8 +425,8 @@ var current; | ||
/* istanbul ignore if - stdin */ | ||
if (!file.filePath()) { | ||
if (!filePath) { | ||
return; | ||
} | ||
references = gatherReferences(file, project); | ||
current = getPathname(file.filePath()); | ||
references = gatherReferences(file, ast, project); | ||
current = getPathname(filePath); | ||
@@ -489,6 +442,20 @@ for (link in references) { | ||
links.push(pathname); | ||
fileSet.add(pathname); | ||
} | ||
} | ||
landmarks[filePath] = true; | ||
visit(ast, function (node) { | ||
var data = node.data || {}; | ||
var attrs = data.htmlAttributes || {}; | ||
var id = attrs.name || attrs.id || data.id; | ||
if (id) { | ||
landmarks[filePath + '#' + id] = true; | ||
} | ||
}); | ||
space.references = references; | ||
space.landmarks = landmarks; | ||
} | ||
@@ -550,3 +517,3 @@ | ||
fileSet.use(completerFactory(repo)); | ||
fileSet.use(completer); | ||
@@ -553,0 +520,0 @@ /* |
{ | ||
"name": "remark-validate-links", | ||
"version": "3.0.0", | ||
"version": "4.0.0-alpha.1", | ||
"description": "Validate links to headings and files in markdown", | ||
@@ -16,7 +16,8 @@ "license": "MIT", | ||
"github-url-to-object": "^2.1.0", | ||
"mdast-util-definitions": "^1.0.0", | ||
"propose": "0.0.5", | ||
"remark-slug": "^4.1.0", | ||
"mdast-util-definitions": "^1.0.0", | ||
"unist-util-visit": "^1.0.0", | ||
"propose": "0.0.5", | ||
"urljoin": "^0.1.5" | ||
"urljoin": "^0.1.5", | ||
"xtend": "^4.0.1" | ||
}, | ||
@@ -37,13 +38,20 @@ "repository": { | ||
"eslint": "^2.0.0", | ||
"hook-std": "^0.2.0", | ||
"execa": "^0.4.0", | ||
"istanbul": "^0.4.0", | ||
"jscs": "^2.0.0", | ||
"jscs-jsdoc": "^1.0.0", | ||
"remark": "^4.0.0-alpha.1", | ||
"remark-comment-config": "^3.0.0", | ||
"jscs": "^3.0.0", | ||
"jscs-jsdoc": "^2.0.0", | ||
"remark": "^5.0.0", | ||
"remark-cli": "^1.0.0", | ||
"remark-comment-config": "^4.0.0-alpha.1", | ||
"remark-github": "^4.0.1", | ||
"remark-lint": "^3.0.0", | ||
"remark-lint": "^4.0.0", | ||
"remark-toc": "^3.0.0", | ||
"tape": "^4.0.0" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"scripts": { | ||
@@ -56,6 +64,5 @@ "build-md": "remark . --quiet --frail", | ||
"test-api": "node test/index.js", | ||
"test-coverage": "istanbul cover test/index.js", | ||
"test-travis": "npm run test-coverage", | ||
"test-coverage": "nyc --reporter lcov tape test/index.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
} | ||
} |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
20665
7
12
4
438
2
+ Addedxtend@^4.0.1
+ Addedxtend@4.0.2(transitive)