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

View file

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

View file

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

View file

@ -23,7 +23,7 @@ Text
## Heading 2b {MD019} {MD022} ## Heading 2b {MD019} {MD022}
- Text *text* text * text * text ** text ** text `text` text ` text ` text {MD004} {MD007} {MD032} {MD037} {MD038} - 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> </p>

View file

@ -3514,6 +3514,46 @@ Generated by [AVA](https://avajs.dev).
'no-bare-urls', '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-', errorContext: 'https://example.com#heading-',
errorDetail: null, errorDetail: null,
@ -3647,12 +3687,12 @@ Generated by [AVA](https://avajs.dev).
<div> <div>
https://example.com <https://example.com> {MD034}
</div> </div>
<div> <div>
https://example.com <https://example.com> {MD034}
</div> </div>
@ -33207,6 +33247,26 @@ Generated by [AVA](https://avajs.dev).
'blanks-around-lists', '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', errorContext: '* t',
errorDetail: null, errorDetail: null,
@ -33350,7 +33410,7 @@ Generated by [AVA](https://avajs.dev).
## Heading 2b {MD019} {MD022}␊ ## Heading 2b {MD019} {MD022}␊
+ Text *text* text *text* text **text** text \`text\` text \`text\` text {MD004} {MD007} {MD032} {MD037} {MD038}␊ + 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> </p>