2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2024-06-29 16:45:40 -07:00
|
|
|
const { addErrorContext } = require("../helpers");
|
2024-08-24 22:05:16 -07:00
|
|
|
const { getDescendantsByType } = require("../helpers/micromark.cjs");
|
|
|
|
const { getReferenceLinkImageData, filterByTypesCached } = require("./cache");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD042", "no-empty-links" ],
|
|
|
|
"description": "No empty links",
|
|
|
|
"tags": [ "links" ],
|
2024-06-29 16:45:40 -07:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD042(params, onError) {
|
2024-08-24 22:05:16 -07:00
|
|
|
const { definitions } = getReferenceLinkImageData();
|
2024-06-29 16:45:40 -07:00
|
|
|
const isReferenceDefinitionHash = (token) => {
|
|
|
|
const definition = definitions.get(token.text.trim());
|
|
|
|
return (definition && (definition[1] === "#"));
|
|
|
|
};
|
2024-08-24 22:05:16 -07:00
|
|
|
const links = filterByTypesCached([ "link" ]);
|
2024-06-29 16:45:40 -07:00
|
|
|
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;
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2024-06-29 16:45:40 -07:00
|
|
|
if (error) {
|
|
|
|
addErrorContext(
|
|
|
|
onError,
|
|
|
|
link.startLine,
|
|
|
|
link.text,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
[ link.startColumn, link.endColumn - link.startColumn ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|