forked from DavidAnson/markdownlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd042.js
63 lines (60 loc) · 2.56 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// @ts-check
"use strict";
const { addErrorContext } = require("../helpers");
const { getDescendantsByType } = require("../helpers/micromark.cjs");
const { getReferenceLinkImageData, filterByTypesCached } = require("./cache");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
module.exports = {
"names": [ "MD042", "no-empty-links" ],
"description": "No empty links",
"tags": [ "links" ],
"parser": "micromark",
"function": function MD042(params, onError) {
const { definitions } = getReferenceLinkImageData();
const isReferenceDefinitionHash = (token) => {
const definition = definitions.get(token.text.trim());
return (definition && (definition[1] === "#"));
};
const links = filterByTypesCached([ "link" ]);
for (const link of links) {
const labelText = getDescendantsByType(link, [ "label", "labelText" ]);
const reference = getDescendantsByType(link, [ "reference" ]);
const resource = getDescendantsByType(link, [ "resource" ]);
const referenceString = getDescendantsByType(reference, [ "referenceString" ]);
const resourceDestination = getDescendantsByType(resource, [ "resourceDestination" ]);
const resourceDestinationString = [
...getDescendantsByType(resourceDestination, [ "resourceDestinationRaw", "resourceDestinationString" ]),
...getDescendantsByType(resourceDestination, [ "resourceDestinationLiteral", "resourceDestinationString" ])
];
const hasLabelText = labelText.length > 0;
const hasReference = reference.length > 0;
const hasResource = resource.length > 0;
const hasReferenceString = referenceString.length > 0;
const hasResourceDestinationString = resourceDestinationString.length > 0;
let error = false;
if (
hasLabelText &&
((!hasReference && !hasResource) || (hasReference && !hasReferenceString))
) {
error = isReferenceDefinitionHash(labelText[0]);
} else if (hasReferenceString && !hasResourceDestinationString) {
error = isReferenceDefinitionHash(referenceString[0]);
} else if (!hasReferenceString && hasResourceDestinationString) {
error = (resourceDestinationString[0].text.trim() === "#");
} else if (!hasReferenceString && !hasResourceDestinationString) {
error = true;
}
if (error) {
addErrorContext(
onError,
link.startLine,
link.text,
undefined,
undefined,
[ link.startColumn, link.endColumn - link.startColumn ]
);
}
}
}
};