From dd66a33d759dc9b62fd7f25890e773a24ad3bb28 Mon Sep 17 00:00:00 2001 From: David Anson Date: Wed, 1 Apr 2020 20:21:36 -0700 Subject: [PATCH] Update MD034/no-bare-urls to ignore "[link]" scenario that conflicts with MD011/no-reversed-links (fixes #268). --- doc/Rules.md | 12 ++++-- helpers/helpers.js | 2 +- lib/md034.js | 47 ++++++++++++++---------- test/detailed-results-links.md | 2 + test/detailed-results-links.md.fixed | 2 + test/detailed-results-links.results.json | 9 +++++ test/links.md | 5 +++ test/reversed_link.md | 8 ++-- 8 files changed, 60 insertions(+), 27 deletions(-) diff --git a/doc/Rules.md b/doc/Rules.md index 3c2552b1..d4419fc9 100644 --- a/doc/Rules.md +++ b/doc/Rules.md @@ -1248,14 +1248,20 @@ To fix this, add angle brackets around the URL: For more information, see . ``` -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. diff --git a/helpers/helpers.js b/helpers/helpers.js index 71d7bfc4..24fea984 100644 --- a/helpers/helpers.js +++ b/helpers/helpers.js @@ -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+)[.)]/; diff --git a/lib/md034.js b/lib/md034.js index bc264773..06e8dbf6 100644 --- a/lib/md034.js +++ b/lib/md034.js @@ -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 + ); + } } } }); diff --git a/test/detailed-results-links.md b/test/detailed-results-links.md index 0928701e..3548c6c5 100644 --- a/test/detailed-results-links.md +++ b/test/detailed-results-links.md @@ -26,3 +26,5 @@ Text more text https://example.com/same still more te Text more \* text https://example.com/same more \[ text 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/] diff --git a/test/detailed-results-links.md.fixed b/test/detailed-results-links.md.fixed index c7a5d40f..90f735cf 100644 --- a/test/detailed-results-links.md.fixed +++ b/test/detailed-results-links.md.fixed @@ -26,3 +26,5 @@ Text more text still more Text more \* text https://example.com/same more \[ text done Text more text still more text done + +[Incorrect link syntax](https://www.example.com/) diff --git a/test/detailed-results-links.results.json b/test/detailed-results-links.results.json index 05b4eaaf..9c173032 100644 --- a/test/detailed-results-links.results.json +++ b/test/detailed-results-links.results.json @@ -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" ], diff --git a/test/links.md b/test/links.md index 4dadc6d9..3bca8676 100644 --- a/test/links.md +++ b/test/links.md @@ -11,3 +11,8 @@ hTtPs://gOoGlE.cOm/ {MD034} ftp://user:password@ftp-server.example.com/dir/file.txt {MD034} This link should be fine: + +The following are allowed to avoid conflicts with MD011/no-reversed-links: + +[https://example.com] +[https://example.com/search?query=text] diff --git a/test/reversed_link.md b/test/reversed_link.md index b6df2601..a5eebbc8 100644 --- a/test/reversed_link.md +++ b/test/reversed_link.md @@ -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} -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`