Merge pull request #235 from matrix-org/matthew/intercept-vector-links

linkify vector.im URLs directly into the app, both from HTML and non-HTML messages
This commit is contained in:
Matthew Hodgson 2016-03-21 16:16:47 +00:00
commit 0d31f8cb63
2 changed files with 41 additions and 9 deletions

View file

@ -19,6 +19,7 @@ limitations under the License.
var React = require('react');
var sanitizeHtml = require('sanitize-html');
var highlight = require('highlight.js');
var linkifyMatrix = require('./linkify-matrix');
var sanitizeHtmlParams = {
allowedTags: [
@ -44,8 +45,17 @@ var sanitizeHtmlParams = {
allowedSchemesByTag: {},
transformTags: { // custom to matrix
// add blank targets to all hyperlinks
'a': sanitizeHtml.simpleTransform('a', { target: '_blank'} )
// add blank targets to all hyperlinks except vector URLs
'a': function(tagName, attribs) {
var m = attribs.href ? attribs.href.match(linkifyMatrix.VECTOR_URL_PATTERN) : null;
if (m) {
delete attribs.target;
}
else {
attribs.target = '_blank';
}
return attribs;
},
},
};

View file

@ -98,6 +98,16 @@ function matrixLinkify(linkify) {
matrixLinkify.onUserClick = function(e, userId) { e.preventDefault(); };
matrixLinkify.onAliasClick = function(e, roomAlias) { e.preventDefault(); };
var escapeRegExp = function(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
};
// we only recognise URLs which match our current URL as being the same app
// as if someone explicitly links to vector.im/develop and we're on vector.im/beta
// they may well be trying to get us to explicitly go to develop.
// FIXME: intercept matrix.to URLs as well.
matrixLinkify.VECTOR_URL_PATTERN = "^(https?:\/\/)?" + escapeRegExp(window.location.host + window.location.pathname);
matrixLinkify.options = {
events: function (href, type) {
switch (type) {
@ -118,14 +128,26 @@ matrixLinkify.options = {
formatHref: function (href, type) {
switch (type) {
case 'roomalias':
return '#/room/' + href;
case 'userid':
return '#';
default:
return href;
case 'roomalias':
return '#/room/' + href;
case 'userid':
return '#';
default:
return href;
}
}
},
target: function(href, type) {
if (type === 'url') {
if (href.match(matrixLinkify.VECTOR_URL_PATTERN)) {
return null;
}
else {
return '_blank';
}
}
return null;
},
};
module.exports = matrixLinkify;