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,17 +5163,18 @@ 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 children = token.children;
var result = []; var result = [];
for (var i = 0; i < flattened.length; i++) { for (var i = 0; i < children.length; i++) {
var current = flattened[i]; var current = children[i];
var openTagInfo = getHtmlTagInfo(current); var openTagInfo = getHtmlTagInfo(current);
if (openTagInfo && !openTagInfo.close) { if (openTagInfo && !openTagInfo.close) {
var count = 1; var count = 1;
for (var j = i + 1; j < flattened.length; j++) { for (var j = i + 1; j < children.length; j++) {
var candidate = flattened[j]; var candidate = children[j];
var closeTagInfo = getHtmlTagInfo(candidate); var closeTagInfo = getHtmlTagInfo(candidate);
if (closeTagInfo && openTagInfo.name === closeTagInfo.name) { if (closeTagInfo && openTagInfo.name === closeTagInfo.name) {
if (closeTagInfo.close) { if (closeTagInfo.close) {
@ -5190,8 +5192,7 @@ module.exports = {
result.push(current); result.push(current);
} }
} }
return result.filter(function (token) { return result;
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,16 +11,20 @@ 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(
tokens,
(token) => (token.type === "literalAutolink") && !inHtmlFlow(token),
(token) => {
const { children } = token;
const result = []; const result = [];
for (let i = 0; i < flattened.length; i++) { for (let i = 0; i < children.length; i++) {
const current = flattened[i]; const current = children[i];
const openTagInfo = getHtmlTagInfo(current); const openTagInfo = getHtmlTagInfo(current);
if (openTagInfo && !openTagInfo.close) { if (openTagInfo && !openTagInfo.close) {
let count = 1; let count = 1;
for (let j = i + 1; j < flattened.length; j++) { for (let j = i + 1; j < children.length; j++) {
const candidate = flattened[j]; const candidate = children[j];
const closeTagInfo = getHtmlTagInfo(candidate); const closeTagInfo = getHtmlTagInfo(candidate);
if (closeTagInfo && (openTagInfo.name === closeTagInfo.name)) { if (closeTagInfo && (openTagInfo.name === closeTagInfo.name)) {
if (closeTagInfo.close) { if (closeTagInfo.close) {
@ -38,8 +42,10 @@ module.exports = {
result.push(current); result.push(current);
} }
} }
return result.filter((token) => token.type === "literalAutolink"); return result;
}; }
)
);
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>