mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
48 lines
1.5 KiB
JavaScript
48 lines
1.5 KiB
JavaScript
// @ts-check
|
|
|
|
"use strict";
|
|
|
|
const { addError, forEachLine, withinAnyRange } = require("../helpers");
|
|
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
|
|
|
|
const reversedLinkRe =
|
|
/(^|[^\\])\(([^()]+)\)\[([^\]^][^\]]*)\](?!\()/g;
|
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
/** @type import("./markdownlint").Rule */
|
|
module.exports = {
|
|
"names": [ "MD011", "no-reversed-links" ],
|
|
"description": "Reversed link syntax",
|
|
"tags": [ "links" ],
|
|
"function": function MD011(params, onError) {
|
|
const exclusions = codeBlockAndSpanRanges();
|
|
forEachLine(lineMetadata(), (line, lineIndex, inCode, onFence) => {
|
|
if (!inCode && !onFence) {
|
|
let match = null;
|
|
while ((match = reversedLinkRe.exec(line)) !== null) {
|
|
const [ reversedLink, preChar, linkText, linkDestination ] = match;
|
|
const index = match.index + preChar.length;
|
|
const length = match[0].length - preChar.length;
|
|
if (
|
|
!linkText.endsWith("\\") &&
|
|
!linkDestination.endsWith("\\") &&
|
|
!withinAnyRange(exclusions, lineIndex, index, length)
|
|
) {
|
|
addError(
|
|
onError,
|
|
lineIndex + 1,
|
|
reversedLink.slice(preChar.length),
|
|
undefined,
|
|
[ index + 1, length ],
|
|
{
|
|
"editColumn": index + 1,
|
|
"deleteCount": length,
|
|
"insertText": `[${linkText}](${linkDestination})`
|
|
}
|
|
);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
};
|