2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2022-06-02 22:17:32 -07:00
|
|
|
const { addError, forEachLine, withinAnyRange } = require("../helpers");
|
2021-11-23 04:40:05 +00:00
|
|
|
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2021-06-17 21:50:03 -07:00
|
|
|
const reversedLinkRe =
|
2021-08-22 22:26:12 -07:00
|
|
|
/(^|[^\\])\(([^)]+)\)\[([^\]^][^\]]*)](?!\()/g;
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD011", "no-reversed-links" ],
|
|
|
|
"description": "Reversed link syntax",
|
|
|
|
"tags": [ "links" ],
|
|
|
|
"function": function MD011(params, onError) {
|
2021-11-23 04:40:05 +00:00
|
|
|
const exclusions = codeBlockAndSpanRanges();
|
2021-06-17 21:50:03 -07:00
|
|
|
forEachLine(lineMetadata(), (line, lineIndex, inCode, onFence) => {
|
|
|
|
if (!inCode && !onFence) {
|
|
|
|
let match = null;
|
|
|
|
while ((match = reversedLinkRe.exec(line)) !== null) {
|
2021-08-22 22:26:12 -07:00
|
|
|
const [ reversedLink, preChar, linkText, linkDestination ] = match;
|
|
|
|
const index = match.index + preChar.length;
|
|
|
|
const length = match[0].length - preChar.length;
|
|
|
|
if (
|
|
|
|
!linkText.endsWith("\\") &&
|
|
|
|
!linkDestination.endsWith("\\") &&
|
2022-06-02 22:17:32 -07:00
|
|
|
!withinAnyRange(exclusions, lineIndex, index, length)
|
2021-08-22 22:26:12 -07:00
|
|
|
) {
|
2021-06-17 21:50:03 -07:00
|
|
|
addError(
|
|
|
|
onError,
|
|
|
|
lineIndex + 1,
|
2021-08-22 22:26:12 -07:00
|
|
|
reversedLink.slice(preChar.length),
|
2021-06-17 21:50:03 -07:00
|
|
|
null,
|
|
|
|
[ index + 1, length ],
|
|
|
|
{
|
|
|
|
"editColumn": index + 1,
|
|
|
|
"deleteCount": length,
|
|
|
|
"insertText": `[${linkText}](${linkDestination})`
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|