mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
feat(openLinksInNewWindow): add option to open all links in a new window
Closes #362, #337, #249, #247, #222
This commit is contained in:
parent
0c6c07b341
commit
50235d6951
|
@ -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
BIN
dist/showdown.js
vendored
Binary file not shown.
BIN
dist/showdown.js.map
vendored
BIN
dist/showdown.js.map
vendored
Binary file not shown.
BIN
dist/showdown.min.js
vendored
BIN
dist/showdown.min.js
vendored
Binary file not shown.
BIN
dist/showdown.min.js.map
vendored
BIN
dist/showdown.min.js.map
vendored
Binary file not shown.
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
test/features/openLinksInNewWindow/simple.html
Normal file
2
test/features/openLinksInNewWindow/simple.html
Normal 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>
|
3
test/features/openLinksInNewWindow/simple.md
Normal file
3
test/features/openLinksInNewWindow/simple.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
[foo](www.google.com)
|
||||
|
||||
a link <http://www.google.com>
|
|
@ -0,0 +1 @@
|
|||
<p>this is <a href="http://www.google.com" target="_blank">http://www.google.com</a> autolink</p>
|
1
test/features/openLinksInNewWindow/simplifiedAutoLink.md
Normal file
1
test/features/openLinksInNewWindow/simplifiedAutoLink.md
Normal file
|
@ -0,0 +1 @@
|
|||
this is http://www.google.com autolink
|
|
@ -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));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user