documentation/lib/output/util/linker_stack.js
Tom MacWright 3f08c19533 Move documentation-theme-utils into core
Also:

* Fixes many issues in the default theme
* Moves the default theme from default-theme to default_theme
* Creates a new, robust linker infrastructure that will support
  bare links
* Replaces slugg with github-slugger
* Outputs unknown types as Any. Fixes #451
2016-06-08 12:40:05 -04:00

78 lines
1.8 KiB
JavaScript

var globalsDocs = require('globals-docs');
var walk = require('../../walk');
/**
* Generate a linker method that links given hardcoded namepaths to URLs
*
* @param {Object} paths an object specified in documentation.yml of hard paths
* @returns {Function} linker
*/
function pathsLinker(paths) {
return function (namespace) {
if (paths[namespace]) {
return paths[namespace];
}
};
}
/**
* Given an array of functions, call each of them with `input`
* and return the output of the first one that returns a truthy value.
*
* @param {Array<Function>} fns array of methods
* @param {*} input any input
* @returns {*} any output
*/
function firstPass(fns, input) {
for (var i = 0; i < fns.length; i++) {
var output = fns[i](input);
if (output) {
return output;
}
}
}
/**
* Create a linking method that takes a namepath and returns a URI
* or a falsy value
*
* @param {Object} config - configuration value
* @returns {Function} linker method
*/
function LinkerStack(config) {
this.stack = [];
if (config.defaultGlobals !== false) {
this.stack.push(function (namespace) {
return globalsDocs.getDoc(namespace, config.defaultGlobalsEnvs);
});
}
if (config.paths) {
this.stack.push(pathsLinker(config.paths));
}
this.link = this.link.bind(this);
}
LinkerStack.prototype.namespaceResolver = function (comments, resolver) {
var namespaces = {};
walk(comments, function (comment) {
namespaces[comment.namespace] = true;
});
this.stack.push(function (namespace) {
if (namespaces[namespace] === true) {
return resolver(namespace);
}
});
return this;
};
LinkerStack.prototype.link = function (namepath) {
return firstPass(this.stack, namepath);
};
module.exports = function (opts) {
return new LinkerStack(opts);
};