Skip to content

Commit

Permalink
Update for changes in [email protected]
Browse files Browse the repository at this point in the history
And faster performance, improved tests, dependency updates.
  • Loading branch information
wooorm committed Jun 13, 2016
1 parent 4d88bc6 commit 72c5aa2
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 333 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
14 changes: 10 additions & 4 deletions .jscs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
"coverage/",
"node_modules/"
],
"preset": "yandex",
"requireQuotedKeysInObjects": true,
"disallowQuotedKeysInObjects": false,
"preset": "crockford",
"requireMultipleVarDecl": false,
"requireVarDeclFirst": false,
"disallowDanglingUnderscores": false,
"maximumLineLength": {
"value": 79,
"allExcept": [
"regex",
"urlComments"
"urlComments",
"require"
]
},
"requireQuotedKeysInObjects": true,
"disallowKeywords": [
"with"
],
"jsDoc": {
"checkAnnotations": "jsdoc3",
"checkParamExistence": true,
Expand Down
179 changes: 73 additions & 106 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var definitions = require('mdast-util-definitions');
var gh = require('github-url-to-object');
var urljoin = require('urljoin');
var slug = require('remark-slug');
var xtend = require('xtend');

/*
* Methods.
Expand All @@ -31,6 +32,12 @@ var slug = require('remark-slug');
var exists = fs.existsSync;
var parse = url.parse;

/*
* Constants.
*/

var NS = 'remark-validate-links';

/**
* Get the `pathname` of `uri`, if applicable.
*
Expand Down Expand Up @@ -110,25 +117,24 @@ function warnAll(file, nodes, warning) {
* to be one or more nodes.
*
* @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
* project, with a `user` and `repo` property
* (optional).
* @return {Object.<string, Array.<Node>>} exposed - Map of
* file-paths (and anchors) which are referenced.
*/
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);

if (project.user && project.repo) {
prefix = '/' + project.user + '/' + project.repo + '/blob/';
Expand Down Expand Up @@ -185,10 +191,17 @@ function gatherReferences(file, project) {
link = filePath + uri.hash;
uri = parse(link);
} 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]);

/*
* Handle full links.
Expand Down Expand Up @@ -254,64 +267,12 @@ function gatherReferences(file, project) {
}
}

visit(ast, 'link', onlink);
visit(ast, 'linkReference', onlink);
visit(tree, 'link', onlink);
visit(tree, 'linkReference', onlink);

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
* `exposed`. If `project` is given, normalizes GitHub blob
Expand All @@ -323,14 +284,9 @@ function gatherExposedFactory() {
* @param {Object.<string, boolean?>} exposed - Map of
* file-paths (and anchors) which can be references to.
* @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();
var reference;
var nodes;
Expand All @@ -345,6 +301,10 @@ function validate(exposed, file, project) {
real = exposed[reference];
hash = getHash(reference);

// if (hash) {
// console.error(['hash', reference, hash]);
// }

/*
* Check if files without `hash` can be linked to.
* Because there’s no need to inspect those files
Expand Down Expand Up @@ -383,50 +343,40 @@ function validate(exposed, file, project) {
}

/**
* 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) {
/* istanbul ignore else - stdin */
if (file.filePath()) {
validate(gatherExposed.cache, file, project);
}
});
set.valueOf().forEach(function (file) {
var landmarks = file.namespace(NS).landmarks;

done();
}
if (landmarks) {
exposed = xtend(exposed, landmarks);
}
});

completer.pluginId = 'remark-validate-links';
set.valueOf().forEach(function (file) {
/* istanbul ignore else - stdin */
if (file.filePath()) {
validate(exposed, file);
}
});

return completer;
done();
}

completer.pluginId = 'remark-validate-links';

/**
* Factory to create a transformer based on the given
* project and set.
Expand All @@ -451,19 +401,22 @@ function transformerFactory(project, fileSet) {
* @param {File} file - Virtual file.
*/
function transformer(ast, file) {
var filePath = file.filePath();
var space = file.namespace(NS);
var links = [];
var landmarks = {};
var references;
var current;
var link;
var pathname;

/* 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);

for (link in references) {
pathname = getPathname(link);
Expand All @@ -474,10 +427,24 @@ function transformerFactory(project, fileSet) {
links.indexOf(pathname) === -1
) {
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;
}

return transformer;
Expand Down Expand Up @@ -534,7 +501,7 @@ function attacher(remark, options, fileSet) {
* Attach a `completer`.
*/

fileSet.use(completerFactory(repo));
fileSet.use(completer);

/*
* Attach `slug`.
Expand Down
31 changes: 19 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
],
"dependencies": {
"github-url-to-object": "^2.1.0",
"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"
"remark-slug": "^4.1.0",
"unist-util-visit": "^1.0.0",
"urljoin": "^0.1.5",
"xtend": "^4.0.1"
},
"repository": {
"type": "git",
Expand All @@ -33,26 +34,32 @@
],
"devDependencies": {
"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",
"remark-github": "^4.0.1",
"remark-lint": "^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",
"remark-github": "^5.0.0-alpha.1",
"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": {
"build-md": "remark . --quiet --frail",
"build": "npm run build-md",
"lint-api": "eslint .",
"lint-style": "jscs --reporter inline .",
"lint": "npm run lint-api && npm run lint-style",
"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"
}
}
Loading

0 comments on commit 72c5aa2

Please sign in to comment.