Skip to content

Make linkify links open in new tab/window #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 4, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions addons/linkify/linkify.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@
* "linkified".
* @param {boolean} lenient - The regex type that will be used to identify links. If lenient is
* false, the regex requires a protocol clause. Defaults to true.
* @param {string} target - Sets target="" attribute with value provided to links.
* Default doesn't set target attribute
* @emits linkify
* @emits linkify:line
*/
exports.linkifyTerminalLine = function (terminal, line, lenient) {
exports.linkifyTerminalLine = function (terminal, line, lenient, target) {
if (typeof line == 'number') {
line = terminal.rowContainer.children[line];
} else if (! (line instanceof HTMLDivElement)) {
Expand All @@ -67,6 +69,12 @@
throw new TypeError(message);
}

if (typeof target === 'undefined') {
target = '';
} else {
target = 'target="' + target + '"';
}

var buffer = document.createElement('span'),
nodes = line.childNodes;

Expand Down Expand Up @@ -100,7 +108,7 @@
var startsWithProtocol = new RegExp('^' + protocolClause),
urlHasProtocol = url.match(startsWithProtocol),
href = (urlHasProtocol) ? url : 'http://' + url,
link = '<a href="' + href + '" >' + url + '</a>',
link = '<a href="' + href + '" ' + target + '>' + url + '</a>',
newHTML = nodeHTML.replace(url, link);

line.innerHTML = line.innerHTML.replace(nodeHTML, newHTML);
Expand Down Expand Up @@ -137,17 +145,19 @@
* @param {Xterm} terminal - The terminal that should get "linkified".
* @param {boolean} lenient - The regex type that will be used to identify links. If lenient is
* false, the regex requires a protocol clause. Defaults to true.
* @param {string} target - Sets target="" attribute with value provided to links.
* Default doesn't set target attribute
* @emits linkify
* @emits linkify:line
*/
exports.linkify = function (terminal, lenient) {
exports.linkify = function (terminal, lenient, target) {
var rows = terminal.rowContainer.children;

lenient = (typeof lenient == "boolean") ? lenient : true;
for (var i=0; i<rows.length; i++) {
var line = rows[i];

exports.linkifyTerminalLine(terminal, line, lenient);
exports.linkifyTerminalLine(terminal, line, lenient, target);
}

/**
Expand All @@ -173,9 +183,11 @@
* "linkified".
* @param {boolean} lenient - The regex type that will be used to identify links. If lenient is
* false, the regex requires a protocol clause. Defaults to true.
* @param {string} target - Sets target="" attribute with value provided to links.
* Default doesn't set target attribute
*/
Xterm.prototype.linkifyTerminalLine = function (line, lenient) {
return exports.linkifyTerminalLine(this, line, lenient);
Xterm.prototype.linkifyTerminalLine = function (line, lenient, target) {
return exports.linkifyTerminalLine(this, line, lenient, target);
};

/**
Expand All @@ -184,9 +196,11 @@
* @memberof Xterm
* @param {boolean} lenient - The regex type that will be used to identify links. If lenient is
* false, the regex requires a protocol clause. Defaults to true.
* @param {string} target - Sets target="" attribute with value provided to links.
* Default doesn't set target attribute
*/
Xterm.prototype.linkify = function (lenient) {
return exports.linkify(this, lenient);
Xterm.prototype.linkify = function (lenient, target) {
return exports.linkify(this, lenient, target);
};

return exports;
Expand Down