Update MD039/no-space-in-links to remove outdated validLink helper and simplify slightly.

This commit is contained in:
David Anson 2025-01-06 20:07:36 -08:00
parent d4352c7a44
commit 637975f517

View file

@ -1,7 +1,7 @@
// @ts-check // @ts-check
import { addErrorContext } from "../helpers/helpers.cjs"; import { addErrorContext } from "../helpers/helpers.cjs";
import { getReferenceLinkImageData, filterByTypesCached } from "./cache.mjs"; import { filterByTypesCached } from "./cache.mjs";
/** /**
* Adds an error for a label space issue. * Adds an error for a label space issue.
@ -35,18 +35,6 @@ function addLabelSpaceError(onError, label, labelText, isStart) {
); );
} }
/**
* Determines if a link is a valid link (and not a fake shortcut link due to parser tricks).
*
* @param {import("markdownlint").MicromarkToken} label Label token.
* @param {import("markdownlint").MicromarkToken} labelText LabelText token.
* @param {Map<string, any>} definitions Map of link definitions.
* @returns {boolean} True iff the link is valid.
*/
function validLink(label, labelText, definitions) {
return (label.parent?.children.length !== 1) || definitions.has(labelText.text.trim());
}
/** @type {import("markdownlint").Rule} */ /** @type {import("markdownlint").Rule} */
export default { export default {
"names": [ "MD039", "no-space-in-links" ], "names": [ "MD039", "no-space-in-links" ],
@ -54,22 +42,15 @@ export default {
"tags": [ "whitespace", "links" ], "tags": [ "whitespace", "links" ],
"parser": "micromark", "parser": "micromark",
"function": function MD039(params, onError) { "function": function MD039(params, onError) {
const { definitions } = getReferenceLinkImageData();
const labels = filterByTypesCached([ "label" ]) const labels = filterByTypesCached([ "label" ])
.filter((label) => label.parent?.type === "link"); .filter((label) => label.parent?.type === "link");
for (const label of labels) { for (const label of labels) {
const labelTexts = label.children.filter((child) => child.type === "labelText"); const labelTexts = label.children.filter((child) => child.type === "labelText");
for (const labelText of labelTexts) { for (const labelText of labelTexts) {
if ( if (labelText.text.trimStart().length !== labelText.text.length) {
(labelText.text.trimStart().length !== labelText.text.length) &&
validLink(label, labelText, definitions)
) {
addLabelSpaceError(onError, label, labelText, true); addLabelSpaceError(onError, label, labelText, true);
} }
if ( if (labelText.text.trimEnd().length !== labelText.text.length) {
(labelText.text.trimEnd().length !== labelText.text.length) &&
validLink(label, labelText, definitions)
) {
addLabelSpaceError(onError, label, labelText, false); addLabelSpaceError(onError, label, labelText, false);
} }
} }