mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Re-implement MD011/no-reversed-links for better accuracy (range and fixInfo are now always valid) (fixes #398).
This commit is contained in:
parent
646a67b8bd
commit
706f48bd25
6 changed files with 165 additions and 91 deletions
53
lib/md011.js
53
lib/md011.js
|
@ -2,40 +2,41 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addError, forEachInlineChild, unescapeMarkdown } =
|
||||
const { addError, forEachLine, inlineCodeSpanRanges, overlapsAnyRange } =
|
||||
require("../helpers");
|
||||
const { lineMetadata } = require("./cache");
|
||||
|
||||
const reversedLinkRe = /\(([^)]+)\)\[([^\]^][^\]]*)]/g;
|
||||
const reversedLinkRe =
|
||||
/(?<![\\\]])\(([^)]+)(?<!\\)\)\[([^\]^][^\]]*)(?<!\\)](?!\()/g;
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD011", "no-reversed-links" ],
|
||||
"description": "Reversed link syntax",
|
||||
"tags": [ "links" ],
|
||||
"function": function MD011(params, onError) {
|
||||
forEachInlineChild(params, "text", (token) => {
|
||||
const { lineNumber, content } = token;
|
||||
let match = null;
|
||||
while ((match = reversedLinkRe.exec(content)) !== null) {
|
||||
const [ reversedLink, linkText, linkDestination ] = match;
|
||||
const line = params.lines[lineNumber - 1];
|
||||
const column = unescapeMarkdown(line).indexOf(reversedLink) + 1;
|
||||
const length = reversedLink.length;
|
||||
const range = column ? [ column, length ] : null;
|
||||
const fixInfo = column ?
|
||||
{
|
||||
"editColumn": column,
|
||||
"deleteCount": length,
|
||||
"insertText": `[${linkText}](${linkDestination})`
|
||||
} :
|
||||
null;
|
||||
addError(
|
||||
onError,
|
||||
lineNumber,
|
||||
reversedLink,
|
||||
null,
|
||||
range,
|
||||
fixInfo
|
||||
);
|
||||
const exclusions = inlineCodeSpanRanges(params.lines);
|
||||
forEachLine(lineMetadata(), (line, lineIndex, inCode, onFence) => {
|
||||
if (!inCode && !onFence) {
|
||||
let match = null;
|
||||
while ((match = reversedLinkRe.exec(line)) !== null) {
|
||||
const [ reversedLink, linkText, linkDestination ] = match;
|
||||
const index = match.index;
|
||||
const length = match[0].length;
|
||||
if (!overlapsAnyRange(exclusions, lineIndex, index, length)) {
|
||||
addError(
|
||||
onError,
|
||||
lineIndex + 1,
|
||||
reversedLink,
|
||||
null,
|
||||
[ index + 1, length ],
|
||||
{
|
||||
"editColumn": index + 1,
|
||||
"deleteCount": length,
|
||||
"insertText": `[${linkText}](${linkDestination})`
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue