Skip to content

Commit 0cc55b0

Browse files
committed
fix(simplifiedAutoLink): fix simplified autolink to match GFM behavior
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
1 parent 984942e commit 0cc55b0

10 files changed

Lines changed: 37 additions & 13 deletions

dist/showdown.js

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/subParsers/autoLinks.js

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

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

1616
if (options.simplifiedAutoLink) {
17-
text = text.replace(simpleURLRegex, '<a href=\"$1\">$1</a>');
17+
text = text.replace(simpleURLRegex, replaceLink);
1818
text = text.replace(simpleMailRegex, replaceMail);
1919
}
2020

21+
function replaceLink(wm, link) {
22+
var lnkTxt = link;
23+
if (/^www\./i.test(link)) {
24+
link = link.replace(/^www\./i, 'http://www.');
25+
}
26+
return '<a href="' + link + '">' + lnkTxt + '</a>';
27+
}
28+
2129
function replaceMail(wholeMatch, m1) {
2230
var unescapedStr = showdown.subParser('unescapeSpecialChars')(m1);
2331
return showdown.subParser('encodeEmailAddress')(unescapedStr);

test/features/#164.1.simple-autolink.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p>www.foobar</p>
44

5-
<p><a href="www.foobar.com">www.foobar.com</a></p>
5+
<p><a href="http://www.foobar.com">www.foobar.com</a></p>
66

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>this is a link to <a href="http://www.github.com">www.github.com</a></p>
2+
3+
<p>this is a link to <a href="http://www.google.com">www.google.com</a></p>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
this is a link to www.github.com
2+
3+
this is a link to <www.google.com>

test/features/tables/with-span-elements.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<td><a href="www.google.com">google</a></td>
2020
</tr>
2121
<tr>
22-
<td><a href="www.foo.com">www.foo.com</a></td>
22+
<td><a href="http://www.foo.com">www.foo.com</a></td>
2323
<td>normal</td>
2424
</tr>
2525
</tbody>

test/node/testsuite.features.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ describe('makeHtml() features testsuite', function () {
3131
converter = new showdown.Converter({literalMidWordUnderscores: true});
3232
} else if (testsuite[i].name === '#259.es6-template-strings-indentation-issues') {
3333
converter = new showdown.Converter({smartIndentationFix: true});
34+
} else if (testsuite[i].name === '#284.simplifiedAutoLink-does-not-match-GFM-style') {
35+
converter = new showdown.Converter({simplifiedAutoLink: true});
3436
} else {
3537
converter = new showdown.Converter();
3638
}

0 commit comments

Comments
 (0)