-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
344 lines (279 loc) · 7.78 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
'use strict';
var url = require('url');
var fs = require('fs');
var path = require('path');
var propose = require('propose');
var visit = require('unist-util-visit');
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');
module.exports = attacher;
completer.pluginId = 'remark-validate-links';
var exists = fs.existsSync;
var parse = url.parse;
function attacher(options, fileSet) {
var repo = (options || {}).repository;
var pack;
/* Throw when not on the CLI. */
if (!fileSet) {
throw new Error('remark-validate-links only works on the CLI');
}
/* Try to get the repo from `package.json` when not
* given. */
if (!repo) {
try {
pack = fileSet.files[0].cwd;
// eslint-disable-next-line import/no-dynamic-require
pack = require(path.resolve(pack, 'package.json'));
} catch (err) {
pack = {};
}
repo = pack.repository ? pack.repository.url || pack.repository : '';
}
repo = repo ? gh(repo) : {};
/* Attach a `completer`. */
fileSet.use(completer);
/* Attach `slug`. */
this.use(slug).use(subplugin);
function subplugin() {
/* Expose transformer. */
return transformerFactory({user: repo.user, repo: repo.repo}, fileSet);
}
}
/* Completer. */
function completer(set, done) {
var exposed = {};
set.valueOf().forEach(function (file) {
var landmarks = file.data.remarkValidateLinksLandmarks;
if (landmarks) {
exposed = xtend(exposed, landmarks);
}
});
set.valueOf().forEach(function (file) {
/* istanbul ignore else - stdin */
if (file.path) {
validate(exposed, file);
}
});
done();
}
/* Factory to create a transformer based on the given
* project and set. */
function transformerFactory(project, fileSet) {
return transformer;
/* Transformer. Adds references files to the set. */
function transformer(ast, file) {
var filePath = file.path;
var space = file.data;
var links = [];
var landmarks = {};
var references;
var current;
var link;
var pathname;
/* istanbul ignore if - stdin */
if (!filePath) {
return;
}
references = gatherReferences(file, ast, project);
current = getPathname(filePath);
for (link in references) {
pathname = getPathname(link);
if (
pathname !== current &&
getHash(link) &&
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.remarkValidateLinksReferences = references;
space.remarkValidateLinksLandmarks = landmarks;
}
}
/* Check if `file` references headings or files not in
* `exposed`. If `project` is given, normalizes GitHub blob
* URLs. */
function validate(exposed, file) {
var references = file.data.remarkValidateLinksReferences;
var filePath = file.path;
var reference;
var nodes;
var real;
var hash;
var pathname;
var warning;
var suggestion;
for (reference in references) {
nodes = references[reference];
real = exposed[reference];
hash = getHash(reference);
/* Check if files without `hash` can be linked to.
* Because there’s no need to inspect those files
* for headings they are not added to remark. This
* is especially useful because they might be
* non-markdown files. Here we check if they exist. */
if ((real === undefined || real === null) && !hash) {
real = references[reference] = exists(reference);
}
if (!real) {
if (hash) {
pathname = getPathname(reference);
warning = 'Link to unknown heading';
if (pathname !== filePath) {
warning += ' in `' + pathname + '`';
}
warning += ': `' + hash + '`';
} else {
warning = 'Link to unknown file: `' + reference + '`';
}
suggestion = getClosest(reference, exposed);
if (suggestion) {
warning += '. Did you mean `' + suggestion + '`';
}
warnAll(file, nodes, warning);
}
}
}
/* Gather references: a map of file-paths references
* to be one or more nodes. */
function gatherReferences(file, tree, project) {
var cache = {};
var filePath = file.path;
var dirname = file.dirname;
var getDefinition;
var prefix = '';
getDefinition = definitions(tree);
if (project.user && project.repo) {
prefix = '/' + project.user + '/' + project.repo + '/blob/';
}
/* Handle new links. */
function onlink(node) {
var link = node.url;
var definition;
var index;
var uri;
var pathname;
var hash;
/* Handle link-references. */
if (node.identifier) {
definition = getDefinition(node.identifier);
link = definition && definition.url;
}
/* Ignore definitions without link. */
if (!link) {
return;
}
uri = parse(link);
if (!uri.hostname) {
/* Handle hashes, or relative files. */
if (!uri.pathname && uri.hash) {
link = filePath + uri.hash;
uri = parse(link);
} else {
link = urljoin(dirname, link);
if (uri.hash) {
link += uri.hash;
}
uri = parse(link);
}
}
/* Handle full links.
* Only works with GitHub.
*/
if (uri.hostname) {
if (!prefix) {
return;
}
if (
uri.hostname !== 'github.com' ||
uri.pathname.slice(0, prefix.length) !== prefix
) {
return;
}
link = uri.pathname.slice(prefix.length) + (uri.hash || '');
/* Things get interesting here: branches.
* `foo/bar/baz` could be `baz` on the
* `foo/bar` branch. Or, `baz` in the `bar`
* directory on the `foo` branch.
*
* Currently, I’m ignoring this and just not
* supporting branches. */
link = link.slice(link.indexOf('/') + 1);
uri = parse(link);
}
/* Handle file links, or combinations of files
* and hashes. */
index = link.indexOf('#');
if (index === -1) {
pathname = link;
hash = null;
} else {
pathname = link.slice(0, index);
hash = link.slice(index + 1);
}
if (!cache[pathname]) {
cache[pathname] = [];
}
cache[pathname].push(node);
if (hash) {
if (!cache[link]) {
cache[link] = [];
}
cache[link].push(node);
}
}
visit(tree, 'link', onlink);
visit(tree, 'linkReference', onlink);
return cache;
}
/* Utilitity to warn `reason` for each node in `nodes`,
* on `file`. */
function warnAll(file, nodes, reason) {
nodes.forEach(function (node) {
var message = file.message(reason, node);
message.source = message.ruleId = 'remark-validate-links';
});
}
/* Suggest a possible similar reference. */
function getClosest(pathname, references) {
var hash = getHash(pathname);
var base = getPathname(pathname);
var dictionary = [];
var reference;
var subhash;
var subbase;
for (reference in references) {
subbase = getPathname(reference);
subhash = getHash(reference);
if (getPathname(reference) === base) {
if (subhash && hash) {
dictionary.push(subhash);
}
} else if (!subhash && !hash) {
dictionary.push(subbase);
}
}
return propose(hash ? hash : base, dictionary, {threshold: 0.7});
}
/* Get the `hash` of `uri`, if applicable. */
function getHash(uri) {
var hash = parse(uri).hash;
return hash ? hash.slice(1) : null;
}
/* Get the `pathname` of `uri`, if applicable. */
function getPathname(uri) {
return parse(uri).pathname;
}