-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
110 lines (93 loc) · 2.94 KB
/
utils.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
define(exports, 'engine', () => require('engine'));
define(exports, 'mdu', () => require('markdown-utils'));
define(exports, 'reflinks', () => require('reflinks'));
define(exports, 'union', () => require('arr-union'));
function define(obj, key, fn) {
Reflect.defineProperty(obj, key, { get: fn });
}
exports.render = function(str, context, options) {
const ctx = Object.assign({}, options, context);
const engine = exports.engine();
if (typeof ctx.truncate !== 'function') {
ctx.truncate = exports.truncate;
}
engine.helper('truncate', ctx.truncate);
engine.helper('link_github', githubLink);
engine.helper('link_npm', npmLink);
return engine.render(str, ctx);
};
function githubLink(name, pkg, options) {
const opts = Object.assign({}, options);
if (typeof opts.githubLink === 'function') {
return opts.githubLink(name, pkg, opts);
}
let desc = pkg.description;
if (typeof opts.description === 'string') {
desc = opts.description;
}
if (opts.description === false) {
desc = '';
}
return exports.mdu.link('homepage', pkg.homepage, desc);
}
function npmLink(name, pkg, options) {
const opts = Object.assign({}, options);
const url = `https://www.npmjs.com/package/${pkg.name}`;
if (typeof opts.githubLink !== 'function') {
return exports.mdu.link('npm', url);
}
return exports.mdu.link('npm', url);
}
exports.arrayify = function(val) {
return val ? (Array.isArray(val) ? val : [val]) : [];
};
exports.isString = function(val) {
return val && typeof val === 'string';
};
exports.filter = function(arr, names) {
names = exports.arrayify(names);
return arr.filter(function(ele) {
return names.indexOf(ele) === -1;
});
};
exports.truncate = function(str, options) {
if (!exports.isString(str)) {
return '';
}
// ensure helper context is available
const ctx = Object.assign({}, this);
const opts = Object.assign({}, options, ctx);
if (opts.words || !opts.chars) {
return exports.truncateWords(str, opts);
}
return exports.truncateString(str, opts);
};
exports.truncateWords = function(str, options) {
const opts = Object.assign({words: 15}, options);
const originalLength = str.length;
const words = str.split(' ').slice(0, opts.words);
const res = words.join(' ');
if (res.length < originalLength) {
return exports.ellipsis(res.replace(/^\W+|\W+$/g, ''), opts.homepage);
}
return res;
};
exports.truncateString = function(str, options) {
const opts = Object.assign({chars: 50}, options);
const originalLength = str.length;
const regex = new RegExp('.{1,' + opts.chars + '}(\\s+|$)|\\S+?(\\s+|$)', 'g');
const lines = str.match(regex) || [];
const res = lines[0];
if (res.length < originalLength) {
return exports.ellipsis(res.replace(/^\W+|\W+$/g, ''), opts.homepage);
}
return res;
};
exports.ellipsis = function(str, href) {
return str + (href ? '… [more](' + href + ')' : '…');
};
/**
* Expose exports
*/
module.exports = exports;