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

@ -5155,6 +5155,7 @@ var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/
filterByPredicate = _require2.filterByPredicate,
filterByTypes = _require2.filterByTypes,
getHtmlTagInfo = _require2.getHtmlTagInfo,
inHtmlFlow = _require2.inHtmlFlow,
parse = _require2.parse;
module.exports = {
"names": ["MD034", "no-bare-urls"],
@ -5162,36 +5163,36 @@ module.exports = {
"tags": ["links", "url"],
"function": function MD034(params, onError) {
var literalAutolinks = function literalAutolinks(tokens) {
var flattened = filterByPredicate(tokens, function () {
return true;
});
var result = [];
for (var i = 0; i < flattened.length; i++) {
var current = flattened[i];
var openTagInfo = getHtmlTagInfo(current);
if (openTagInfo && !openTagInfo.close) {
var count = 1;
for (var j = i + 1; j < flattened.length; j++) {
var candidate = flattened[j];
var closeTagInfo = getHtmlTagInfo(candidate);
if (closeTagInfo && openTagInfo.name === closeTagInfo.name) {
if (closeTagInfo.close) {
count--;
if (count === 0) {
i = j;
break;
return filterByPredicate(tokens, function (token) {
return token.type === "literalAutolink" && !inHtmlFlow(token);
}, function (token) {
var children = token.children;
var result = [];
for (var i = 0; i < children.length; i++) {
var current = children[i];
var openTagInfo = getHtmlTagInfo(current);
if (openTagInfo && !openTagInfo.close) {
var count = 1;
for (var j = i + 1; j < children.length; j++) {
var candidate = children[j];
var 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.filter(function (token) {
return token.type === "literalAutolink";
return result;
});
};
var autoLinks = filterByTypes(params.parsers.micromark.tokens, ["literalAutolink"]);

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" ]

View file

@ -70,12 +70,12 @@ https://example.com
<div>
https://example.com
https://example.com {MD034}
</div>
<div>
https://example.com
https://example.com {MD034}
</div>

View file

@ -23,7 +23,7 @@ Text
## Heading 2b {MD019} {MD022}
- Text *text* text * text * text ** text ** text `text` text ` text ` text {MD004} {MD007} {MD032} {MD037} {MD038}
- Text https://example.com/ [ link ](https://example.com/) {MD004} {MD005} {MD039}
- Text https://example.com/ [ link ](https://example.com/) {MD004} {MD005} {MD034} {MD039}
</p>

View file

@ -3514,6 +3514,46 @@ Generated by [AVA](https://avajs.dev).
'no-bare-urls',
],
},
{
errorContext: 'https://example.com',
errorDetail: null,
errorRange: [
1,
19,
],
fixInfo: {
deleteCount: 19,
editColumn: 1,
insertText: '<https://example.com>',
},
lineNumber: 73,
ruleDescription: 'Bare URL used',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md034.md',
ruleNames: [
'MD034',
'no-bare-urls',
],
},
{
errorContext: 'https://example.com',
errorDetail: null,
errorRange: [
1,
19,
],
fixInfo: {
deleteCount: 19,
editColumn: 1,
insertText: '<https://example.com>',
},
lineNumber: 78,
ruleDescription: 'Bare URL used',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md034.md',
ruleNames: [
'MD034',
'no-bare-urls',
],
},
{
errorContext: 'https://example.com#heading-',
errorDetail: null,
@ -3647,12 +3687,12 @@ Generated by [AVA](https://avajs.dev).
<div>
https://example.com
<https://example.com> {MD034}
</div>
<div>
https://example.com
<https://example.com> {MD034}
</div>
@ -33207,6 +33247,26 @@ Generated by [AVA](https://avajs.dev).
'blanks-around-lists',
],
},
{
errorContext: 'https://example.com/',
errorDetail: null,
errorRange: [
8,
20,
],
fixInfo: {
deleteCount: 20,
editColumn: 8,
insertText: '<https://example.com/>',
},
lineNumber: 26,
ruleDescription: 'Bare URL used',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md034.md',
ruleNames: [
'MD034',
'no-bare-urls',
],
},
{
errorContext: '* t',
errorDetail: null,
@ -33350,7 +33410,7 @@ Generated by [AVA](https://avajs.dev).
## Heading 2b {MD019} {MD022}␊
+ Text *text* text *text* text **text** text \`text\` text \`text\` text {MD004} {MD007} {MD032} {MD037} {MD038}␊
+ Text https://example.com/ [link](https://example.com/) {MD004} {MD005} {MD039}␊
+ Text <https://example.com/> [link](https://example.com/) {MD004} {MD005} {MD034} {MD039}␊
</p>