Update MD034/no-bare-urls to restore per-sibling scan (vs. all-element scan) and ignore tokens inside an htmlFlow context.

This commit is contained in:
David Anson 2023-10-23 20:55:37 -07:00
parent a084c2525c
commit 739cfb6fe2
6 changed files with 123 additions and 56 deletions

View file

@ -3,7 +3,7 @@
"use strict";
const { addErrorContext } = require("../helpers");
const { filterByPredicate, filterByTypes, getHtmlTagInfo, parse } =
const { filterByPredicate, filterByTypes, getHtmlTagInfo, inHtmlFlow, parse } =
require("../helpers/micromark.cjs");
module.exports = {
@ -11,35 +11,41 @@ module.exports = {
"description": "Bare URL used",
"tags": [ "links", "url" ],
"function": function MD034(params, onError) {
const literalAutolinks = (tokens) => {
const flattened = filterByPredicate(tokens, () => true);
const result = [];
for (let i = 0; i < flattened.length; i++) {
const current = flattened[i];
const openTagInfo = getHtmlTagInfo(current);
if (openTagInfo && !openTagInfo.close) {
let count = 1;
for (let j = i + 1; j < flattened.length; j++) {
const candidate = flattened[j];
const closeTagInfo = getHtmlTagInfo(candidate);
if (closeTagInfo && (openTagInfo.name === closeTagInfo.name)) {
if (closeTagInfo.close) {
count--;
if (count === 0) {
i = j;
break;
const literalAutolinks = (tokens) => (
filterByPredicate(
tokens,
(token) => (token.type === "literalAutolink") && !inHtmlFlow(token),
(token) => {
const { children } = token;
const result = [];
for (let i = 0; i < children.length; i++) {
const current = children[i];
const openTagInfo = getHtmlTagInfo(current);
if (openTagInfo && !openTagInfo.close) {
let count = 1;
for (let j = i + 1; j < children.length; j++) {
const candidate = children[j];
const closeTagInfo = getHtmlTagInfo(candidate);
if (closeTagInfo && (openTagInfo.name === closeTagInfo.name)) {
if (closeTagInfo.close) {
count--;
if (count === 0) {
i = j;
break;
}
} else {
count++;
}
}
} else {
count++;
}
} else {
result.push(current);
}
}
} else {
result.push(current);
return result;
}
}
return result.filter((token) => token.type === "literalAutolink");
};
)
);
const autoLinks = filterByTypes(
params.parsers.micromark.tokens,
[ "literalAutolink" ]