-
-
Notifications
You must be signed in to change notification settings - Fork 746
/
Copy pathmd042.mjs
56 lines (54 loc) · 2.33 KB
/
md042.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
// @ts-check
import { addErrorContext } from "../helpers/helpers.cjs";
import { getDescendantsByType } from "../helpers/micromark-helpers.cjs";
import { getReferenceLinkImageData, filterByTypesCached } from "./cache.mjs";
/** @type {import("markdownlint").Rule} */
export default {
"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 resourceDestinationString = getDescendantsByType(resource, [ "resourceDestination", [ "resourceDestinationLiteral", "resourceDestinationRaw" ], "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 ]
);
}
}
}
};