Skip to content

Commit

Permalink
fix(simplifiedAutoLink): fix simplified autolink to match GFM behavior
Browse files Browse the repository at this point in the history
Using the simplifiedAutoLink option does not return the expected GFM behaviour when parsing links without a http prefix.
Previously, `www.google.com` would be parsed into `<a href="www.google.com">www.google.com</a>`.
With this fix, showdown behaves like GFM, and the result is `<a href="http://www.google.com">www.google.com</a>`

Closes #284, closes #285
  • Loading branch information
tivie committed Aug 19, 2016
1 parent 984942e commit 0cc55b0
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 13 deletions.
16 changes: 12 additions & 4 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions src/subParsers/autoLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ showdown.subParser('autoLinks', function (text, options, globals) {
simpleMailRegex = /(?:^|[ \n\t])([A-Za-z0-9!#$%&'*+-/=?^_`\{|}~\.]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(?:$|[ \n\t])/gi,
delimMailRegex = /<(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi;

text = text.replace(delimUrlRegex, '<a href=\"$1\">$1</a>');
text = text.replace(delimUrlRegex, replaceLink);
text = text.replace(delimMailRegex, replaceMail);
//simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[-.+~:?#@!$&'()*,;=[\]\w]+)\b/gi,
// simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[-.+~:?#@!$&'()*,;=[\]\w]+)\b/gi,
// Email addresses: <address@domain.foo>

if (options.simplifiedAutoLink) {
text = text.replace(simpleURLRegex, '<a href=\"$1\">$1</a>');
text = text.replace(simpleURLRegex, replaceLink);
text = text.replace(simpleMailRegex, replaceMail);
}

function replaceLink(wm, link) {
var lnkTxt = link;
if (/^www\./i.test(link)) {
link = link.replace(/^www\./i, 'http://www.');
}
return '<a href="' + link + '">' + lnkTxt + '</a>';
}

function replaceMail(wholeMatch, m1) {
var unescapedStr = showdown.subParser('unescapeSpecialChars')(m1);
return showdown.subParser('encodeEmailAddress')(unescapedStr);
Expand Down
2 changes: 1 addition & 1 deletion test/features/#164.1.simple-autolink.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p>www.foobar</p>

<p><a href="www.foobar.com">www.foobar.com</a></p>
<p><a href="http://www.foobar.com">www.foobar.com</a></p>

<p><a href="http://foobar.com">http://foobar.com</a></p>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>this is a link to <a href="http://www.github.com">www.github.com</a></p>

<p>this is a link to <a href="http://www.google.com">www.google.com</a></p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
this is a link to www.github.com

this is a link to <www.google.com>
2 changes: 1 addition & 1 deletion test/features/tables/with-span-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<td><a href="www.google.com">google</a></td>
</tr>
<tr>
<td><a href="www.foo.com">www.foo.com</a></td>
<td><a href="http://www.foo.com">www.foo.com</a></td>
<td>normal</td>
</tr>
</tbody>
Expand Down
2 changes: 2 additions & 0 deletions test/node/testsuite.features.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ describe('makeHtml() features testsuite', function () {
converter = new showdown.Converter({literalMidWordUnderscores: true});
} else if (testsuite[i].name === '#259.es6-template-strings-indentation-issues') {
converter = new showdown.Converter({smartIndentationFix: true});
} else if (testsuite[i].name === '#284.simplifiedAutoLink-does-not-match-GFM-style') {
converter = new showdown.Converter({simplifiedAutoLink: true});
} else {
converter = new showdown.Converter();
}
Expand Down

1 comment on commit 0cc55b0

@ynnoj
Copy link

@ynnoj ynnoj commented on 0cc55b0 Aug 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 🎉

Please sign in to comment.