Skip to content

Commit

Permalink
Update MD039/no-space-in-links to remove outdated validLink helper an…
Browse files Browse the repository at this point in the history
…d simplify slightly.
  • Loading branch information
DavidAnson committed Jan 7, 2025
1 parent 74b1c19 commit 961adb9
Showing 1 changed file with 3 additions and 22 deletions.
25 changes: 3 additions & 22 deletions lib/md039.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check

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.
Expand Down Expand Up @@ -35,41 +35,22 @@ 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} */
export default {
"names": [ "MD039", "no-space-in-links" ],
"description": "Spaces inside link text",
"tags": [ "whitespace", "links" ],
"parser": "micromark",
"function": function MD039(params, onError) {
const { definitions } = getReferenceLinkImageData();
const labels = filterByTypesCached([ "label" ])
.filter((label) => label.parent?.type === "link");
for (const label of labels) {
const labelTexts = label.children.filter((child) => child.type === "labelText");
for (const labelText of labelTexts) {
if (
(labelText.text.trimStart().length !== labelText.text.length) &&
validLink(label, labelText, definitions)
) {
if (labelText.text.trimStart().length !== labelText.text.length) {
addLabelSpaceError(onError, label, labelText, true);
}
if (
(labelText.text.trimEnd().length !== labelText.text.length) &&
validLink(label, labelText, definitions)
) {
if (labelText.text.trimEnd().length !== labelText.text.length) {
addLabelSpaceError(onError, label, labelText, false);
}
}
Expand Down

0 comments on commit 961adb9

Please sign in to comment.