Selaa lähdekoodia

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
Estevao Soares dos Santos 9 vuotta sitten
vanhempi
sitoutus
0cc55b07ee

+ 12 - 4
dist/showdown.js

@@ -1,4 +1,4 @@
-;/*! showdown 20-07-2016 */
+;/*! showdown 19-08-2016 */
 (function(){
 /**
  * Created by Tivie on 13-07-2015.
@@ -1242,16 +1242,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);

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
dist/showdown.js.map


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 1 - 1
dist/showdown.min.js


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
dist/showdown.min.js.map


+ 11 - 3
src/subParsers/autoLinks.js

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

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

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

+ 3 - 0
test/features/#284.simplifiedAutoLink-does-not-match-GFM-style.html

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

+ 3 - 0
test/features/#284.simplifiedAutoLink-does-not-match-GFM-style.md

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

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

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

+ 2 - 0
test/node/testsuite.features.js

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

Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä