Update MD034/no-bare-urls to ignore "[link]" scenario that conflicts with MD011/no-reversed-links (fixes #268).

This commit is contained in:
David Anson 2020-04-01 20:21:36 -07:00
parent 29f16bf402
commit dd66a33d75
8 changed files with 60 additions and 27 deletions

View file

@ -1248,14 +1248,20 @@ To fix this, add angle brackets around the URL:
For more information, see <https://www.example.com/>.
```
Note: if you do want a bare URL without it being converted into a link,
enclose it in a code block, otherwise in some markdown parsers it _will_ be
converted:
Note: To use a bare URL without it being converted into a link, enclose it in
a code block, otherwise in some markdown parsers it _will_ be converted:
```markdown
`https://www.example.com`
```
Note: The following scenario does _not_ trigger this rule to avoid conflicts
with `MD011`/`no-reversed-links`:
```markdown
[https://www.example.com]
```
Rationale: Without angle brackets, the URL isn't converted into a link in many
markdown parsers.

View file

@ -21,7 +21,7 @@ const inlineCommentRe =
module.exports.inlineCommentRe = inlineCommentRe;
// Regular expressions for range matching
module.exports.bareUrlRe = /(?:http|ftp)s?:\/\/[^\s]*/ig;
module.exports.bareUrlRe = /(?:http|ftp)s?:\/\/[^\s\]]*/ig;
module.exports.listItemMarkerRe = /^([\s>]*)(?:[*+-]|\d+[.)])\s+/;
module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;

View file

@ -21,25 +21,34 @@ module.exports = {
} else if ((type === "text") && !inLink) {
while ((match = bareUrlRe.exec(content)) !== null) {
const [ bareUrl ] = match;
const index = line.indexOf(content);
const range = (index === -1) ? null : [
line.indexOf(content) + match.index + 1,
bareUrl.length
];
const fixInfo = range ? {
"editColumn": range[0],
"deleteCount": range[1],
"insertText": `<${bareUrl}>`
} : null;
addErrorContext(
onError,
lineNumber,
bareUrl,
null,
null,
range,
fixInfo
);
const matchIndex = match.index;
const bareUrlLength = bareUrl.length;
// Allow "[https://example.com]" to avoid conflicts with
// MD011/no-reversed-links
if (
(content[matchIndex - 1] !== "[") ||
(content[matchIndex + bareUrlLength] !== "]")
) {
const index = line.indexOf(content);
const range = (index === -1) ? null : [
index + matchIndex + 1,
bareUrlLength
];
const fixInfo = range ? {
"editColumn": range[0],
"deleteCount": range[1],
"insertText": `<${bareUrl}>`
} : null;
addErrorContext(
onError,
lineNumber,
bareUrl,
null,
null,
range,
fixInfo
);
}
}
}
});

View file

@ -26,3 +26,5 @@ Text <https://example.com/same> more text https://example.com/same still more te
Text <https://example.com/same> more \* text https://example.com/same more \[ text <https://example.com/same> done
Text https://example.com/first more text https://example.com/second still more text https://example.com/third done
(Incorrect link syntax)[https://www.example.com/]

View file

@ -26,3 +26,5 @@ Text <https://example.com/same> more text <https://example.com/same> still more
Text <https://example.com/same> more \* text https://example.com/same more \[ text <https://example.com/same> done
Text <https://example.com/first> more text <https://example.com/second> still more text <https://example.com/third> done
[Incorrect link syntax](https://www.example.com/)

View file

@ -1,4 +1,13 @@
[
{
"lineNumber": 30,
"ruleNames": [ "MD011", "no-reversed-links" ],
"ruleDescription": "Reversed link syntax",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md011",
"errorDetail": "(Incorrect link syntax)[https://www.example.com/]",
"errorContext": null,
"errorRange": [ 1, 49 ]
},
{
"lineNumber": 3,
"ruleNames": [ "MD034", "no-bare-urls" ],

View file

@ -11,3 +11,8 @@ hTtPs://gOoGlE.cOm/ {MD034}
ftp://user:password@ftp-server.example.com/dir/file.txt {MD034}
This link should be fine: <https://www.google.com/>
The following are allowed to avoid conflicts with MD011/no-reversed-links:
[https://example.com]
[https://example.com/search?query=text]

View file

@ -1,4 +1,4 @@
Go to (this website)[https://www.example.com] {MD011} {MD034}
Go to (this website)[https://www.example.com] {MD011}
However, this shouldn't trigger inside code blocks:
@ -6,11 +6,11 @@ However, this shouldn't trigger inside code blocks:
Nor inline code: `myobj.getFiles("test")[0]`
Two (issues)[https://www.example.com/one] in {MD011} {MD034}
the (same text)[https://www.example.com/two]. {MD011} {MD034}
Two (issues)[https://www.example.com/one] in {MD011}
the (same text)[https://www.example.com/two]. {MD011}
<!-- markdownlint-disable line-length -->
Two (issues)[https://www.example.com/three] on the (same line)[https://www.example.com/four]. {MD011} {MD034}
Two (issues)[https://www.example.com/three] on the (same line)[https://www.example.com/four]. {MD011}
`code code
code`