markdownlint/lib/md011.mjs
David Anson 7668b0a263
Some checks are pending
Checkers / linkcheck (push) Waiting to run
Checkers / spellcheck (push) Waiting to run
CI / build (20, macos-latest) (push) Waiting to run
CI / build (20, ubuntu-latest) (push) Waiting to run
CI / build (20, windows-latest) (push) Waiting to run
CI / build (22, macos-latest) (push) Waiting to run
CI / build (22, ubuntu-latest) (push) Waiting to run
CI / build (22, windows-latest) (push) Waiting to run
CI / build (24, macos-latest) (push) Waiting to run
CI / build (24, ubuntu-latest) (push) Waiting to run
CI / build (24, windows-latest) (push) Waiting to run
CI / pnpm (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
TestRepos / build (latest, ubuntu-latest) (push) Waiting to run
UpdateTestRepos / update (push) Waiting to run
Update MD011/no-reversed-links to ignore content of math blocks and spans (fixes #1864).
2025-11-17 21:32:32 -08:00

56 lines
2.1 KiB
JavaScript

// @ts-check
import { addError, hasOverlap } from "../helpers/helpers.cjs";
import { addRangeToSet } from "../helpers/micromark-helpers.cjs";
import { filterByTypesCached } from "./cache.mjs";
/** @typedef {import("micromark-extension-math")} */
const reversedLinkRe = /(^|[^\\])\(([^()]+)\)\[([^\]^][^\]]*)\](?!\()/g;
/** @type {import("markdownlint").Rule} */
export default {
"names": [ "MD011", "no-reversed-links" ],
"description": "Reversed link syntax",
"tags": [ "links" ],
"parser": "micromark",
"function": function MD011(params, onError) {
const ignoreBlockLineNumbers = new Set();
for (const ignoreBlock of filterByTypesCached([ "codeFenced", "codeIndented", "mathFlow" ])) {
addRangeToSet(ignoreBlockLineNumbers, ignoreBlock.startLine, ignoreBlock.endLine);
}
const ignoreTexts = filterByTypesCached([ "codeText", "mathText" ]);
for (const [ lineIndex, line ] of params.lines.entries()) {
const lineNumber = lineIndex + 1;
if (!ignoreBlockLineNumbers.has(lineNumber)) {
let match = null;
while ((match = reversedLinkRe.exec(line)) !== null) {
const [ reversedLink, preChar, linkText, linkDestination ] = match;
if (
!linkText.endsWith("\\") &&
!linkDestination.endsWith("\\")
) {
const column = match.index + preChar.length + 1;
const length = match[0].length - preChar.length;
/** @type {import("../helpers/helpers.cjs").FileRange} */
const range = { "startLine": lineNumber, "startColumn": column, "endLine": lineNumber, "endColumn": column + length - 1 };
if (!ignoreTexts.some((ignoreText) => hasOverlap(ignoreText, range))) {
addError(
onError,
lineNumber,
reversedLink.slice(preChar.length),
undefined,
[ column, length ],
{
"editColumn": column,
"deleteCount": length,
"insertText": `[${linkText}](${linkDestination})`
}
);
}
}
}
}
}
}
};