mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Update MD034/no-bare-urls to allow bare URLs inside the link text of HTML A tags (fixes 615).
This commit is contained in:
parent
2e63bf7dd8
commit
f84c91d95f
5 changed files with 99 additions and 8 deletions
|
|
@ -3705,6 +3705,8 @@ module.exports = {
|
||||||
// @ts-check
|
// @ts-check
|
||||||
|
|
||||||
const { addErrorContext, bareUrlRe, filterTokens } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
const { addErrorContext, bareUrlRe, filterTokens } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||||
|
const htmlLinkOpenRe = /^<a[\s>]/i;
|
||||||
|
const htmlLinkCloseRe = /^<\/a[\s>]/i;
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": ["MD034", "no-bare-urls"],
|
"names": ["MD034", "no-bare-urls"],
|
||||||
"description": "Bare URL used",
|
"description": "Bare URL used",
|
||||||
|
|
@ -3712,6 +3714,7 @@ module.exports = {
|
||||||
"function": function MD034(params, onError) {
|
"function": function MD034(params, onError) {
|
||||||
filterTokens(params, "inline", (token) => {
|
filterTokens(params, "inline", (token) => {
|
||||||
let inLink = false;
|
let inLink = false;
|
||||||
|
let inInline = false;
|
||||||
for (const child of token.children) {
|
for (const child of token.children) {
|
||||||
const { content, line, lineNumber, type } = child;
|
const { content, line, lineNumber, type } = child;
|
||||||
let match = null;
|
let match = null;
|
||||||
|
|
@ -3721,14 +3724,19 @@ module.exports = {
|
||||||
else if (type === "link_close") {
|
else if (type === "link_close") {
|
||||||
inLink = false;
|
inLink = false;
|
||||||
}
|
}
|
||||||
else if ((type === "text") && !inLink) {
|
else if ((type === "html_inline") && htmlLinkOpenRe.test(content)) {
|
||||||
|
inInline = true;
|
||||||
|
}
|
||||||
|
else if ((type === "html_inline") && htmlLinkCloseRe.test(content)) {
|
||||||
|
inInline = false;
|
||||||
|
}
|
||||||
|
else if ((type === "text") && !inLink && !inInline) {
|
||||||
while ((match = bareUrlRe.exec(content)) !== null) {
|
while ((match = bareUrlRe.exec(content)) !== null) {
|
||||||
const [bareUrl] = match;
|
const [bareUrl] = match;
|
||||||
const matchIndex = match.index;
|
const matchIndex = match.index;
|
||||||
const bareUrlLength = bareUrl.length;
|
const bareUrlLength = bareUrl.length;
|
||||||
// Allow "[https://example.com]" to avoid conflicts with
|
// Allow "[LINK]" to avoid conflicts with MD011/no-reversed-links
|
||||||
// MD011/no-reversed-links; allow quoting as another way
|
// Allow quoting as a way of deliberately including a bare URL
|
||||||
// of deliberately including a bare URL
|
|
||||||
const leftChar = content[matchIndex - 1];
|
const leftChar = content[matchIndex - 1];
|
||||||
const rightChar = content[matchIndex + bareUrlLength];
|
const rightChar = content[matchIndex + bareUrlLength];
|
||||||
if (!((leftChar === "[") && (rightChar === "]")) &&
|
if (!((leftChar === "[") && (rightChar === "]")) &&
|
||||||
|
|
|
||||||
15
lib/md034.js
15
lib/md034.js
|
|
@ -4,6 +4,9 @@
|
||||||
|
|
||||||
const { addErrorContext, bareUrlRe, filterTokens } = require("../helpers");
|
const { addErrorContext, bareUrlRe, filterTokens } = require("../helpers");
|
||||||
|
|
||||||
|
const htmlLinkOpenRe = /^<a[\s>]/i;
|
||||||
|
const htmlLinkCloseRe = /^<\/a[\s>]/i;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": [ "MD034", "no-bare-urls" ],
|
"names": [ "MD034", "no-bare-urls" ],
|
||||||
"description": "Bare URL used",
|
"description": "Bare URL used",
|
||||||
|
|
@ -11,6 +14,7 @@ module.exports = {
|
||||||
"function": function MD034(params, onError) {
|
"function": function MD034(params, onError) {
|
||||||
filterTokens(params, "inline", (token) => {
|
filterTokens(params, "inline", (token) => {
|
||||||
let inLink = false;
|
let inLink = false;
|
||||||
|
let inInline = false;
|
||||||
for (const child of token.children) {
|
for (const child of token.children) {
|
||||||
const { content, line, lineNumber, type } = child;
|
const { content, line, lineNumber, type } = child;
|
||||||
let match = null;
|
let match = null;
|
||||||
|
|
@ -18,14 +22,17 @@ module.exports = {
|
||||||
inLink = true;
|
inLink = true;
|
||||||
} else if (type === "link_close") {
|
} else if (type === "link_close") {
|
||||||
inLink = false;
|
inLink = false;
|
||||||
} else if ((type === "text") && !inLink) {
|
} else if ((type === "html_inline") && htmlLinkOpenRe.test(content)) {
|
||||||
|
inInline = true;
|
||||||
|
} else if ((type === "html_inline") && htmlLinkCloseRe.test(content)) {
|
||||||
|
inInline = false;
|
||||||
|
} else if ((type === "text") && !inLink && !inInline) {
|
||||||
while ((match = bareUrlRe.exec(content)) !== null) {
|
while ((match = bareUrlRe.exec(content)) !== null) {
|
||||||
const [ bareUrl ] = match;
|
const [ bareUrl ] = match;
|
||||||
const matchIndex = match.index;
|
const matchIndex = match.index;
|
||||||
const bareUrlLength = bareUrl.length;
|
const bareUrlLength = bareUrl.length;
|
||||||
// Allow "[https://example.com]" to avoid conflicts with
|
// Allow "[LINK]" to avoid conflicts with MD011/no-reversed-links
|
||||||
// MD011/no-reversed-links; allow quoting as another way
|
// Allow quoting as a way of deliberately including a bare URL
|
||||||
// of deliberately including a bare URL
|
|
||||||
const leftChar = content[matchIndex - 1];
|
const leftChar = content[matchIndex - 1];
|
||||||
const rightChar = content[matchIndex + bareUrlLength];
|
const rightChar = content[matchIndex + bareUrlLength];
|
||||||
if (
|
if (
|
||||||
|
|
|
||||||
|
|
@ -11,3 +11,21 @@ For more, see https://example.com/info.htm. {MD034}
|
||||||
Visit https://example.com, then refresh. {MD034}
|
Visit https://example.com, then refresh. {MD034}
|
||||||
|
|
||||||
The site (https://example.com) is down. {MD034}
|
The site (https://example.com) is down. {MD034}
|
||||||
|
|
||||||
|
<!-- markdownlint-disable line-length no-inline-html -->
|
||||||
|
|
||||||
|
Some documents use <a href="https://example.com">to link</a>.
|
||||||
|
|
||||||
|
Or <a href="https://example.com/info.htm">to link</a>.
|
||||||
|
|
||||||
|
Or repeat the URL <a href="https://example.com">https://example.com</a>.
|
||||||
|
|
||||||
|
Or <a href="https://example.com/info.htm">https://example.com/info.htm</a>.
|
||||||
|
|
||||||
|
This is allowed to avoid embedding angle brackets in HTML <a href="https://example.com">Text https://example.com</a>.
|
||||||
|
|
||||||
|
As is <a href="https://example.com/info.htm">https://example.com/info.htm text</a>.
|
||||||
|
|
||||||
|
<br> Another violation: https://example.com. {MD034} <br>
|
||||||
|
|
||||||
|
<br/> Another violation: https://example.com. {MD034} <br/>
|
||||||
|
|
|
||||||
|
|
@ -2926,6 +2926,46 @@ Generated by [AVA](https://avajs.dev).
|
||||||
'no-bare-urls',
|
'no-bare-urls',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
errorContext: 'https://example.com',
|
||||||
|
errorDetail: null,
|
||||||
|
errorRange: [
|
||||||
|
25,
|
||||||
|
19,
|
||||||
|
],
|
||||||
|
fixInfo: {
|
||||||
|
deleteCount: 19,
|
||||||
|
editColumn: 25,
|
||||||
|
insertText: '<https://example.com>',
|
||||||
|
},
|
||||||
|
lineNumber: 29,
|
||||||
|
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: [
|
||||||
|
26,
|
||||||
|
19,
|
||||||
|
],
|
||||||
|
fixInfo: {
|
||||||
|
deleteCount: 19,
|
||||||
|
editColumn: 26,
|
||||||
|
insertText: '<https://example.com>',
|
||||||
|
},
|
||||||
|
lineNumber: 31,
|
||||||
|
ruleDescription: 'Bare URL used',
|
||||||
|
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md034.md',
|
||||||
|
ruleNames: [
|
||||||
|
'MD034',
|
||||||
|
'no-bare-urls',
|
||||||
|
],
|
||||||
|
},
|
||||||
],
|
],
|
||||||
fixed: `# Detailed Results Bare URLs␊
|
fixed: `# Detailed Results Bare URLs␊
|
||||||
␊
|
␊
|
||||||
|
|
@ -2940,6 +2980,24 @@ Generated by [AVA](https://avajs.dev).
|
||||||
Visit <https://example.com>, then refresh. {MD034}␊
|
Visit <https://example.com>, then refresh. {MD034}␊
|
||||||
␊
|
␊
|
||||||
The site (<https://example.com>) is down. {MD034}␊
|
The site (<https://example.com>) is down. {MD034}␊
|
||||||
|
␊
|
||||||
|
<!-- markdownlint-disable line-length no-inline-html -->␊
|
||||||
|
␊
|
||||||
|
Some documents use <a href="https://example.com">to link</a>.␊
|
||||||
|
␊
|
||||||
|
Or <a href="https://example.com/info.htm">to link</a>.␊
|
||||||
|
␊
|
||||||
|
Or repeat the URL <a href="https://example.com">https://example.com</a>.␊
|
||||||
|
␊
|
||||||
|
Or <a href="https://example.com/info.htm">https://example.com/info.htm</a>.␊
|
||||||
|
␊
|
||||||
|
This is allowed to avoid embedding angle brackets in HTML <a href="https://example.com">Text https://example.com</a>.␊
|
||||||
|
␊
|
||||||
|
As is <a href="https://example.com/info.htm">https://example.com/info.htm text</a>.␊
|
||||||
|
␊
|
||||||
|
<br> Another violation: <https://example.com>. {MD034} <br>␊
|
||||||
|
␊
|
||||||
|
<br/> Another violation: <https://example.com>. {MD034} <br/>␊
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue