-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
136 lines (109 loc) · 3.17 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
const validateName = require('validate-npm-package-name');
const expand = require('expand-reflinks');
const reflinks = require('reflinks');
const unique = require('array-unique');
const union = require('arr-union');
const diff = require('arr-diff');
/**
* Lint a markdown string for missing reflinks and append them
* to the end of the string.
*/
module.exports = function(options = {}) {
return function(file, next) {
if (!file.isBuffer()) {
next();
return;
}
let str = expand(file.contents.toString());
let arr = getMatches(str);
if (arr.length === 0) {
next(null, file);
return;
}
file._reflinks = arr;
// add `options.reflinks` to file.contents, but not to `file._reflinks`
let list = union([], options.reflinks, arr);
reflinks(list, options, function(err, res) {
if (err) {
next(err);
return;
}
// filter out reflinks that are already in the file.contents
let links = res.links.filter(function(link) {
return str && str.indexOf(link) === -1;
});
if (links.length === 0) {
next(null, file);
return;
}
str += '\n\n' + links.join('\n');
file.contents = Buffer.from(str);
next(null, file);
});
};
};
function getMatches(str) {
let regex = /(`\[|\[[^\W][^\]]+\]\[\](?!`)|(?!`)\[[^\W][^\]]+\](?![`(\[])|(?!`)\[[^\]]+\]\[[^\]]+\](?!`)|(?!.*[`\]])\[[^\W][^\]]+\](?= |$))/gm;
let matches = (str.match(regex) || []).filter((e, i, arr) => arr.indexOf(e) === i);
let len = matches.length;
let arr = [];
for (let i = 0; i < len; i++) {
let match = matches[i];
if (match.charAt(0) === '`') {
continue;
}
let m = /(\[([^\]]+)\])(\[([^\]]+)\])?/.exec(match);
let name = m[4] || m[2];
if (!name || arr.indexOf(name) !== -1) {
continue;
}
if (!isValidPackageName(name)) {
continue;
}
if (!/^[-a-z0-9.]+$/i.test(name) || /^v?\d+\./.test(name)) {
continue;
}
// don't add the reflink if it already exists
let re = new RegExp(`(^|\\n)\\[${name}\\]: .`, 'gm');
if (re.test(str)) {
continue;
}
arr.push(name);
}
arr.sort();
return arr;
}
/**
* Get the unique reflinks from `app.cache.reflinks` that do not
* already exist on the given `array`. (Use a middleware to add the reflinks on
* `file._reflinks` to `app.cache.reflinks`).
*
* ```js
* app.set('cache.reflinks', ['foo']);
* let diff = reflinks.diff(app, ['foo', 'bar', 'baz']);
* console.log(diff);
* //=> ['bar', baz']
* ```
*
* @param {Object} `app` Instance of [verb], [assemble], [generate], or [update].
* @param {Array} `arr` Array of reflinks remove from the returned array.
* @return {Array}
* @api public
*/
function getDiff(app, existing) {
return diff(app.get('cache.reflinks') || [], existing || []);
}
/**
* Returns true if the given name is a valid npm package name
*/
function isValidPackageName(name) {
const stats = validateName(name);
return stats.validForNewPackages === true
&& stats.validForOldPackages === true;
}
/**
* Expose helper functions as methods
*/
module.exports.matches = getMatches;
module.exports.diff = getDiff;