Update MD034/no-bare-urls to handle more scenarios, simplify slightly, replace blanket MD034 suppression for https://github.com/mdn/content with specific (valid) issues (refs #607).

This commit is contained in:
David Anson 2022-12-15 14:27:07 -08:00
parent 2e2937081e
commit d352d4ece1
6 changed files with 103 additions and 18 deletions

View file

@ -2,7 +2,8 @@
"use strict";
const { addErrorContext, urlRe, withinAnyRange } = require("../helpers");
const { addErrorContext, filterTokens, urlRe, withinAnyRange } =
require("../helpers");
const { codeBlockAndSpanRanges, htmlElementRanges, referenceLinkImageData } =
require("./cache");
@ -18,6 +19,11 @@ module.exports = {
...codeBlockAndSpanRanges(),
...htmlElementRanges()
];
filterTokens(params, "html_block", (token) => {
for (let i = token.map[0]; i < token.map[1]; i++) {
codeExclusions.push([ i, 0, lines[i].length ]);
}
});
const { definitionLineIndices } = referenceLinkImageData();
for (const [ lineIndex, line ] of lines.entries()) {
if (definitionLineIndices[0] === lineIndex) {
@ -35,15 +41,17 @@ module.exports = {
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(">")) &&
// Allow >...</ to avoid reporting <code>...</code>
!(prefix.endsWith(">") && postfix.startsWith("</")) &&
// Allow "..." and '...' to allow quoting a bare link
!(prefix.endsWith("\"") && postfix.startsWith("\"")) &&
!(prefix.endsWith("'") && postfix.startsWith("'")) &&
// Allow ](... to avoid reporting Markdown-style links
!(/\]\(\s*$/.test(prefix)) &&
// Allow [...] to avoid MD011/no-reversed-links and nested links
!(/\[[^\]]*$/.test(prefix) && /^[^[]*\]/.test(postfix)) &&
!withinAnyRange(
lineExclusions, lineIndex, matchIndex, bareUrlLength
) &&