Update MD034/no-bare-urls to better handle multiple similar issues on the same line (fixes https://github.com/igorshubovych/markdownlint-cli/issues/339).

This commit is contained in:
David Anson 2022-12-06 22:14:40 -08:00
parent ce5d393109
commit cba5e8d340
7 changed files with 290 additions and 119 deletions

View file

@ -2,67 +2,76 @@
"use strict";
const { addErrorContext, bareUrlRe, filterTokens } = require("../helpers");
const { addErrorContext, bareUrlRe, withinAnyRange } = require("../helpers");
const { codeBlockAndSpanRanges, htmlElementRanges, referenceLinkImageData } =
require("./cache");
const htmlLinkOpenRe = /^<a[\s>]/i;
const htmlLinkCloseRe = /^<\/a[\s>]/i;
const htmlLinkRe = /<a(?:|\s[^>]+)>[^<>]*<\/a\s*>/ig;
module.exports = {
"names": [ "MD034", "no-bare-urls" ],
"description": "Bare URL used",
"tags": [ "links", "url" ],
"function": function MD034(params, onError) {
filterTokens(params, "inline", (token) => {
let inLink = false;
let inInline = false;
for (const child of token.children) {
const { content, line, lineNumber, type } = child;
const { lines } = params;
const codeExclusions = [
...codeBlockAndSpanRanges(),
...htmlElementRanges()
];
const { definitionLineIndices } = referenceLinkImageData();
for (const [ lineIndex, line ] of lines.entries()) {
if (definitionLineIndices[0] === lineIndex) {
definitionLineIndices.shift();
} else {
let match = null;
if (type === "link_open") {
inLink = true;
} else if (type === "link_close") {
inLink = false;
} else if ((type === "html_inline") && htmlLinkOpenRe.test(content)) {
inInline = true;
} else if ((type === "html_inline") && htmlLinkCloseRe.test(content)) {
inInline = false;
} else if ((type === "text") && !inLink && !inInline) {
while ((match = bareUrlRe.exec(content)) !== null) {
const [ bareUrl ] = match;
const matchIndex = match.index;
const bareUrlLength = bareUrl.length;
// Allow "[LINK]" to avoid conflicts with MD011/no-reversed-links
// Allow quoting as a way of deliberately including a bare URL
const leftChar = content[matchIndex - 1];
const rightChar = content[matchIndex + bareUrlLength];
if (
!((leftChar === "[") && (rightChar === "]")) &&
!((leftChar === "\"") && (rightChar === "\"")) &&
!((leftChar === "'") && (rightChar === "'"))
) {
const index = line.indexOf(content);
const range = (index === -1) ? null : [
index + matchIndex + 1,
bareUrlLength
];
const fixInfo = range ? {
"editColumn": range[0],
"deleteCount": range[1],
"insertText": `<${bareUrl}>`
} : null;
addErrorContext(
onError,
lineNumber,
bareUrl,
null,
null,
range,
fixInfo
);
}
const lineExclusions = [];
while ((match = htmlLinkRe.exec(line)) !== null) {
lineExclusions.push([ lineIndex, match.index, match[0].length ]);
}
while ((match = bareUrlRe.exec(line)) !== null) {
const [ bareUrl ] = match;
const matchIndex = match.index;
const bareUrlLength = bareUrl.length;
const prefix = line.slice(0, matchIndex);
const postfix = line.slice(matchIndex + bareUrlLength);
if (
// Allow ](... to avoid reporting Markdown-style links
!(/\]\(\s*$/.test(prefix)) &&
// Allow <...> to avoid reporting non-bare links
!(prefix.endsWith("<") && /^[#)]?>/.test(postfix)) &&
// Allow [...] to avoid MD011/no-reversed-links and nested links
!(/\[[^\]]*$/.test(prefix) && /^[^[]*\]/.test(postfix)) &&
// Allow "..." and '...' for deliberately including a bare link
!(prefix.endsWith("\"") && postfix.startsWith("\"")) &&
!(prefix.endsWith("'") && postfix.startsWith("'")) &&
!withinAnyRange(
lineExclusions, lineIndex, matchIndex, bareUrlLength
) &&
!withinAnyRange(
codeExclusions, lineIndex, matchIndex, bareUrlLength
)
) {
const range = [
matchIndex + 1,
bareUrlLength
];
const fixInfo = {
"editColumn": range[0],
"deleteCount": range[1],
"insertText": `<${bareUrl}>`
};
addErrorContext(
onError,
lineIndex + 1,
bareUrl,
null,
null,
range,
fixInfo
);
}
}
}
});
}
}
};