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 messagespull/21833/head
commit
0d31f8cb63
|
@ -19,6 +19,7 @@ limitations under the License.
|
||||||
var React = require('react');
|
var React = require('react');
|
||||||
var sanitizeHtml = require('sanitize-html');
|
var sanitizeHtml = require('sanitize-html');
|
||||||
var highlight = require('highlight.js');
|
var highlight = require('highlight.js');
|
||||||
|
var linkifyMatrix = require('./linkify-matrix');
|
||||||
|
|
||||||
var sanitizeHtmlParams = {
|
var sanitizeHtmlParams = {
|
||||||
allowedTags: [
|
allowedTags: [
|
||||||
|
@ -44,8 +45,17 @@ var sanitizeHtmlParams = {
|
||||||
allowedSchemesByTag: {},
|
allowedSchemesByTag: {},
|
||||||
|
|
||||||
transformTags: { // custom to matrix
|
transformTags: { // custom to matrix
|
||||||
// add blank targets to all hyperlinks
|
// add blank targets to all hyperlinks except vector URLs
|
||||||
'a': sanitizeHtml.simpleTransform('a', { target: '_blank'} )
|
'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;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -98,6 +98,16 @@ function matrixLinkify(linkify) {
|
||||||
matrixLinkify.onUserClick = function(e, userId) { e.preventDefault(); };
|
matrixLinkify.onUserClick = function(e, userId) { e.preventDefault(); };
|
||||||
matrixLinkify.onAliasClick = function(e, roomAlias) { 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 = {
|
matrixLinkify.options = {
|
||||||
events: function (href, type) {
|
events: function (href, type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
@ -125,7 +135,19 @@ matrixLinkify.options = {
|
||||||
default:
|
default:
|
||||||
return href;
|
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;
|
module.exports = matrixLinkify;
|
||||||
|
|
Loading…
Reference in New Issue