Browse Source

feat(openLinksInNewWindow): add option to open all links in a new window

Closes #362, #337, #249, #247, #222
Estevao Soares dos Santos 8 năm trước cách đây
mục cha
commit
50235d6951

+ 2 - 0
README.md

@@ -310,6 +310,8 @@ var defaultOptions = showdown.getDefaultOptions();
 
    NOTE: Prior to version 1.6.1, emails would always be obfuscated through dec and hex encoding.
 
+ * **openLinksInNewWindow**: (boolean) [default false] Open all links in new windows (by adding the attribute `target="_blank"` to `<a>` tags)
+
 **NOTE**: Please note that until version 1.6.0, all of these options are ***DISABLED*** by default in the cli tool.
  
 ## Flavors

+ 16 - 3
dist/showdown.js

@@ -1,4 +1,4 @@
-;/*! showdown 30-03-2017 */
+;/*! showdown 31-03-2017 */
 (function(){
 /**
  * Created by Tivie on 13-07-2015.
@@ -117,6 +117,11 @@ function getDefaultOpts (simple) {
       defaultValue: true,
       description: 'Encode e-mail addresses through the use of Character Entities, transforming ASCII e-mail addresses into its equivalent decimal entities',
       type: 'boolean'
+    },
+    openLinksInNewWindow: {
+      defaultValue: false,
+      description: 'Open all links in new windows',
+      type: 'boolean'
     }
   };
   if (simple === false) {
@@ -1347,6 +1352,10 @@ showdown.subParser('anchors', function (text, options, globals) {
       result += ' title="' + title + '"';
     }
 
+    if (options.openLinksInNewWindow) {
+      result += ' target="_blank"';
+    }
+
     result += '>' + linkText + '</a>';
 
     return result;
@@ -1398,14 +1407,18 @@ var simpleURLRegex  = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)()(
 
       return function (wm, link, m2, m3, trailingPunctuation) {
         var lnkTxt = link,
-            append = '';
+            append = '',
+            target = '';
         if (/^www\./i.test(link)) {
           link = link.replace(/^www\./i, 'http://www.');
         }
         if (options.excludeTrailingPunctuationFromURLs && trailingPunctuation) {
           append = trailingPunctuation;
         }
-        return '<a href="' + link + '">' + lnkTxt + '</a>' + append;
+        if (options.openLinksInNewWindow) {
+          target = ' target="_blank"';
+        }
+        return '<a href="' + link + '"' + target + '>' + lnkTxt + '</a>' + append;
       };
     },
 

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/showdown.js.map


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 1 - 1
dist/showdown.min.js


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/showdown.min.js.map


+ 5 - 0
src/options.js

@@ -115,6 +115,11 @@ function getDefaultOpts (simple) {
       defaultValue: true,
       description: 'Encode e-mail addresses through the use of Character Entities, transforming ASCII e-mail addresses into its equivalent decimal entities',
       type: 'boolean'
+    },
+    openLinksInNewWindow: {
+      defaultValue: false,
+      description: 'Open all links in new windows',
+      type: 'boolean'
     }
   };
   if (simple === false) {

+ 4 - 0
src/subParsers/anchors.js

@@ -50,6 +50,10 @@ showdown.subParser('anchors', function (text, options, globals) {
       result += ' title="' + title + '"';
     }
 
+    if (options.openLinksInNewWindow) {
+      result += ' target="_blank"';
+    }
+
     result += '>' + linkText + '</a>';
 
     return result;

+ 6 - 2
src/subParsers/autoLinks.js

@@ -12,14 +12,18 @@ var simpleURLRegex  = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)()(
 
       return function (wm, link, m2, m3, trailingPunctuation) {
         var lnkTxt = link,
-            append = '';
+            append = '',
+            target = '';
         if (/^www\./i.test(link)) {
           link = link.replace(/^www\./i, 'http://www.');
         }
         if (options.excludeTrailingPunctuationFromURLs && trailingPunctuation) {
           append = trailingPunctuation;
         }
-        return '<a href="' + link + '">' + lnkTxt + '</a>' + append;
+        if (options.openLinksInNewWindow) {
+          target = ' target="_blank"';
+        }
+        return '<a href="' + link + '"' + target + '>' + lnkTxt + '</a>' + append;
       };
     },
 

+ 2 - 0
test/features/openLinksInNewWindow/simple.html

@@ -0,0 +1,2 @@
+<p><a href="www.google.com" target="_blank">foo</a></p>
+<p>a link <a href="http://www.google.com" target="_blank">http://www.google.com</a></p>

+ 3 - 0
test/features/openLinksInNewWindow/simple.md

@@ -0,0 +1,3 @@
+[foo](www.google.com)
+
+a link <http://www.google.com>

+ 1 - 0
test/features/openLinksInNewWindow/simplifiedAutoLink.html

@@ -0,0 +1 @@
+<p>this is <a href="http://www.google.com" target="_blank">http://www.google.com</a> autolink</p>

+ 1 - 0
test/features/openLinksInNewWindow/simplifiedAutoLink.md

@@ -0,0 +1 @@
+this is http://www.google.com autolink

+ 15 - 1
test/node/testsuite.features.js

@@ -6,7 +6,8 @@ var bootstrap = require('../bootstrap.js'),
     assertion = bootstrap.assertion,
     testsuite = bootstrap.getTestSuite('test/features/'),
     tableSuite = bootstrap.getTestSuite('test/features/tables/'),
-    simplifiedAutoLinkSuite = bootstrap.getTestSuite('test/features/simplifiedAutoLink/');
+    simplifiedAutoLinkSuite = bootstrap.getTestSuite('test/features/simplifiedAutoLink/'),
+    openLinksInNewWindowSuite = bootstrap.getTestSuite('test/features/openLinksInNewWindow/');
 
 describe('makeHtml() features testsuite', function () {
   'use strict';
@@ -111,4 +112,17 @@ describe('makeHtml() features testsuite', function () {
     }
   });
 
+  // test openLinksInNewWindow support
+  describe('openLinksInNewWindow support in', function () {
+    var converter,
+        suite = openLinksInNewWindowSuite;
+    for (var i = 0; i < suite.length; ++i) {
+      if (suite[i].name === 'simplifiedAutoLink') {
+        converter = new showdown.Converter({openLinksInNewWindow: true, simplifiedAutoLink: true});
+      } else {
+        converter = new showdown.Converter({openLinksInNewWindow: true});
+      }
+      it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
+    }
+  });
 });

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác