-
-
Notifications
You must be signed in to change notification settings - Fork 757
/
Copy pathmd042.js
49 lines (46 loc) · 1.39 KB
/
md042.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
// @ts-check
"use strict";
const { addErrorContext, escapeForRegExp, filterTokens } =
require("../helpers");
module.exports = {
"names": [ "MD042", "no-empty-links" ],
"description": "No empty links",
"tags": [ "links" ],
"function": function MD042(params, onError) {
filterTokens(params, "inline", function forToken(token) {
let inLink = false;
let linkText = "";
let emptyLink = false;
for (const child of token.children) {
if (child.type === "link_open") {
inLink = true;
linkText = "";
for (const attr of child.attrs) {
if (attr[0] === "href" && (!attr[1] || (attr[1] === "#"))) {
emptyLink = true;
}
}
} else if (child.type === "link_close") {
inLink = false;
if (emptyLink) {
let context = `[${linkText}]`;
let range = null;
const match = child.line.match(
new RegExp(`${escapeForRegExp(context)}\\((?:|#|<>)\\)`)
);
if (match) {
context = match[0];
range = [ match.index + 1, match[0].length ];
}
addErrorContext(
onError, child.lineNumber, context, null, null, range
);
emptyLink = false;
}
} else if (inLink) {
linkText += child.content;
}
}
});
}
};