mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Update MD034/no-bare-urls to scan all tokens when pruning HTML content so open/close pairs with different parents are handled (fixes #966).
This commit is contained in:
parent
de5fa400e7
commit
daec896b8d
5 changed files with 100 additions and 60 deletions
60
lib/md034.js
60
lib/md034.js
|
|
@ -3,7 +3,7 @@
|
|||
"use strict";
|
||||
|
||||
const { addErrorContext } = require("../helpers");
|
||||
const { filterByPredicate, getHtmlTagInfo, parse } =
|
||||
const { filterByPredicate, filterByTypes, getHtmlTagInfo, parse } =
|
||||
require("../helpers/micromark.cjs");
|
||||
|
||||
module.exports = {
|
||||
|
|
@ -11,42 +11,40 @@ module.exports = {
|
|||
"description": "Bare URL used",
|
||||
"tags": [ "links", "url" ],
|
||||
"function": function MD034(params, onError) {
|
||||
const literalAutolinks = (tokens) => (
|
||||
filterByPredicate(
|
||||
tokens,
|
||||
(token) => token.type === "literalAutolink",
|
||||
(token) => {
|
||||
const { children } = token;
|
||||
const result = [];
|
||||
for (let i = 0; i < children.length; i++) {
|
||||
const openToken = children[i];
|
||||
const openTagInfo = getHtmlTagInfo(openToken);
|
||||
if (openTagInfo && !openTagInfo.close) {
|
||||
let count = 1;
|
||||
for (let j = i + 1; j < children.length; j++) {
|
||||
const closeToken = children[j];
|
||||
const closeTagInfo = getHtmlTagInfo(closeToken);
|
||||
if (closeTagInfo && (openTagInfo.name === closeTagInfo.name)) {
|
||||
if (closeTagInfo.close) {
|
||||
count--;
|
||||
if (count === 0) {
|
||||
i = j;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
count++;
|
||||
}
|
||||
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;
|
||||
}
|
||||
} else {
|
||||
count++;
|
||||
}
|
||||
} else {
|
||||
result.push(openToken);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
result.push(current);
|
||||
}
|
||||
)
|
||||
}
|
||||
return result.filter((token) => token.type === "literalAutolink");
|
||||
};
|
||||
const autoLinks = filterByTypes(
|
||||
params.parsers.micromark.tokens,
|
||||
[ "literalAutolink" ]
|
||||
);
|
||||
if (literalAutolinks(params.parsers.micromark.tokens).length > 0) {
|
||||
if (autoLinks.length > 0) {
|
||||
// Re-parse with correct link/image reference definition handling
|
||||
const document = params.lines.join("\n");
|
||||
const tokens = parse(document, undefined, false);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue