Skip to content

Commit

Permalink
feat(openLinksInNewWindow): add option to open all links in a new window
Browse files Browse the repository at this point in the history
Closes #362, #337, #249, #247, #222
  • Loading branch information
tivie committed Mar 30, 2017
1 parent 0c6c07b commit 50235d6
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/subParsers/anchors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions src/subParsers/autoLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
},

Expand Down
2 changes: 2 additions & 0 deletions test/features/openLinksInNewWindow/simple.html
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions test/features/openLinksInNewWindow/simple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[foo](www.google.com)

a link <http://www.google.com>
1 change: 1 addition & 0 deletions test/features/openLinksInNewWindow/simplifiedAutoLink.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>this is <a href="http://www.google.com" target="_blank">http://www.google.com</a> autolink</p>
1 change: 1 addition & 0 deletions test/features/openLinksInNewWindow/simplifiedAutoLink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is http://www.google.com autolink
16 changes: 15 additions & 1 deletion test/node/testsuite.features.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
}
});
});

0 comments on commit 50235d6

Please sign in to comment.