-
-
Notifications
You must be signed in to change notification settings - Fork 752
/
Copy pathmd052.mjs
39 lines (37 loc) · 1.36 KB
/
md052.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
// @ts-check
import { addError } from "../helpers/helpers.cjs";
import { getReferenceLinkImageData } from "./cache.mjs";
/** @type {import("markdownlint").Rule} */
export default {
"names": [ "MD052", "reference-links-images" ],
"description":
"Reference links and images should use a label that is defined",
"tags": [ "images", "links" ],
"parser": "none",
"function": function MD052(params, onError) {
const { config, lines } = params;
const shortcutSyntax = config.shortcut_syntax || false;
const { definitions, references, shortcuts } = getReferenceLinkImageData();
const entries = shortcutSyntax ?
[ ...references.entries(), ...shortcuts.entries() ] :
references.entries();
// Look for links/images that use an undefined link reference
for (const reference of entries) {
const [ label, datas ] = reference;
if (!definitions.has(label)) {
for (const data of datas) {
const [ lineIndex, index, length ] = data;
// Context will be incomplete if reporting for a multi-line link
const context = lines[lineIndex].slice(index, index + length);
addError(
onError,
lineIndex + 1,
`Missing link or image reference definition: "${label}"`,
context,
[ index + 1, context.length ]
);
}
}
}
}
};