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
This commit is contained in:
Estevao Soares dos Santos 2016-08-19 19:12:25 +01:00
parent 984942e239
commit 0cc55b07ee
10 changed files with 21 additions and 5 deletions

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

View File

@ -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);

View File

@ -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>

View File

@ -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>

View File

@ -0,0 +1,3 @@
this is a link to www.github.com
this is a link to <www.google.com>

View File

@ -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>

View File

@ -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();
}