-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
72 lines (62 loc) · 1.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
const utils = require('./utils');
const defaults = {
spinnerStart: 'creating related links from npm data',
spinnerStop: 'created related links from npm data',
template: function(pkg, options) {
const opts = Object.assign({description: ''}, options);
const defaultTemplate = '- [<%= name %>](https://www.npmjs.com/package/<%= name %>): <%= truncate(description, 15) %> | <%= link_github(name, obj) %>';
const str = opts.template || defaultTemplate;
return utils.render(str, pkg, opts);
}
};
function relatedList(config) {
return function(names, options, cb) {
if (typeof names === 'function') {
cb = names;
options = {};
names = null;
}
if (typeof options === 'function') {
cb = options;
options = {};
}
const app = this || {};
const opts = Object.assign({}, defaults, config, options, app.options);
const ctx = Object.assign({}, app.context, opts.context);
if (typeof opts.template === 'string') {
const tmpl = opts.template;
opts.template = function(pkg, options) {
return utils.render(tmpl, pkg, options);
};
}
names = utils.filter(utils.arrayify(names), opts.remove);
names = utils.union([], names, opts.names, ctx.names);
names.sort();
if (names.length === 0) {
cb(null, '');
return;
}
utils.reflinks(names, opts, function(err, res) {
if (err) {
cb(err);
return;
}
cb(null, toList(res.links, opts, ctx, names.length));
});
};
}
function toList(links, options, ctx, len) {
const opts = Object.assign({}, options);
if (typeof opts.toList === 'function') {
return opts.toList(links, opts, ctx);
}
if (len === 1 && links && links[0]) {
return links[0].replace(/^[-*+ ]+/, '');
}
return links.join('\n');
}
/**
* Expose `relatedList`
*/
module.exports = relatedList;