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

Closes #362, #337, #249, #247, #222
This commit is contained in:
Estevao Soares dos Santos 2017-03-31 00:25:40 +01:00
parent 0c6c07b341
commit 50235d6951
13 changed files with 39 additions and 3 deletions

View File

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

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

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

View File

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

View File

@ -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;
};
},

View File

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

View File

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

View File

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

View File

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

View File

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