-
-
Notifications
You must be signed in to change notification settings - Fork 752
/
Copy pathmd034.mjs
91 lines (88 loc) · 2.77 KB
/
md034.mjs
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
// @ts-check
import { addErrorContext } from "../helpers/helpers.cjs";
import { filterByPredicate, getHtmlTagInfo, inHtmlFlow } from "../helpers/micromark-helpers.cjs";
/** @typedef {import("micromark-extension-gfm-autolink-literal")} */
/** @type {import("markdownlint").Rule} */
export default {
"names": [ "MD034", "no-bare-urls" ],
"description": "Bare URL used",
"tags": [ "links", "url" ],
"parser": "micromark",
"function": function MD034(params, onError) {
const literalAutolinks = (tokens) => (
filterByPredicate(
tokens,
(token) => {
if ((token.type === "literalAutolink") && !inHtmlFlow(token)) {
// Detect and ignore https://github.com/micromark/micromark/issues/164
const siblings = token.parent?.children;
const index = siblings?.indexOf(token);
// @ts-ignore
const prev = siblings?.at(index - 1);
// @ts-ignore
const next = siblings?.at(index + 1);
return !(
prev &&
next &&
(prev.type === "data") &&
(next.type === "data") &&
prev.text.endsWith("<") &&
next.text.startsWith(">")
);
}
return false;
},
(token) => {
// Ignore content of inline HTML tags
const { children } = token;
const result = [];
for (let i = 0; i < children.length; i++) {
const current = children[i];
const openTagInfo = getHtmlTagInfo(current);
if (openTagInfo && !openTagInfo.close) {
let count = 1;
for (let j = i + 1; j < children.length; j++) {
const candidate = children[j];
const closeTagInfo = getHtmlTagInfo(candidate);
if (closeTagInfo && (openTagInfo.name === closeTagInfo.name)) {
if (closeTagInfo.close) {
count--;
if (count === 0) {
i = j;
break;
}
} else {
count++;
}
}
}
} else {
result.push(current);
}
}
return result;
}
)
);
for (const token of literalAutolinks(params.parsers.micromark.tokens)) {
const range = [
token.startColumn,
token.endColumn - token.startColumn
];
const fixInfo = {
"editColumn": range[0],
"deleteCount": range[1],
"insertText": `<${token.text}>`
};
addErrorContext(
onError,
token.startLine,
token.text,
undefined,
undefined,
range,
fixInfo
);
}
}
};