Update MD034/no-bare-urls to re-scan documents with potential violations using proper reference definition handling to avoid false positives (fixes #787).

This commit is contained in:
David Anson 2023-05-23 04:01:55 +00:00
parent 054f208e9a
commit 488813f7f7
7 changed files with 136 additions and 102 deletions

View file

@ -3,7 +3,7 @@
"use strict";
const { addErrorContext } = require("../helpers");
const { filterByPredicate, getHtmlTagInfo } =
const { filterByPredicate, getHtmlTagInfo, parse } =
require("../helpers/micromark.cjs");
module.exports = {
@ -11,9 +11,9 @@ module.exports = {
"description": "Bare URL used",
"tags": [ "links", "url" ],
"function": function MD034(params, onError) {
const literalAutolinks =
const literalAutolinks = (tokens) => (
filterByPredicate(
params.parsers.micromark.tokens,
tokens,
(token) => token.type === "literalAutolink",
(token) => {
const { children } = token;
@ -43,26 +43,33 @@ module.exports = {
}
}
return result;
});
for (const token of literalAutolinks) {
const range = [
token.startColumn,
token.endColumn - token.startColumn
];
const fixInfo = {
"editColumn": range[0],
"deleteCount": range[1],
"insertText": `<${token.text}>`
};
addErrorContext(
onError,
token.startLine,
token.text,
null,
null,
range,
fixInfo
);
}
)
);
if (literalAutolinks(params.parsers.micromark.tokens).length > 0) {
// Re-parse with correct link/image reference definition handling
const document = params.lines.join("\n");
const tokens = parse(document, undefined, false);
for (const token of literalAutolinks(tokens)) {
const range = [
token.startColumn,
token.endColumn - token.startColumn
];
const fixInfo = {
"editColumn": range[0],
"deleteCount": range[1],
"insertText": `<${token.text}>`
};
addErrorContext(
onError,
token.startLine,
token.text,
null,
null,
range,
fixInfo
);
}
}
}
};