2015-07-10 16:37:13 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2019-10-01 05:17:54 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2015-07-10 16:37:13 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-09-30 21:51:17 +03:00
|
|
|
import {baseUrl} from "./utils/permalinks/SpecPermalinkConstructor";
|
2020-03-20 03:42:44 +03:00
|
|
|
import {
|
|
|
|
parsePermalink,
|
|
|
|
tryTransformEntityToPermalink,
|
2020-03-20 03:56:41 +03:00
|
|
|
tryTransformPermalinkToLocalHref,
|
2020-03-20 03:42:44 +03:00
|
|
|
} from "./utils/permalinks/Permalinks";
|
2017-12-10 15:50:41 +03:00
|
|
|
|
2015-07-10 16:37:13 +03:00
|
|
|
function matrixLinkify(linkify) {
|
|
|
|
// Text tokens
|
2017-10-11 19:56:17 +03:00
|
|
|
const TT = linkify.scanner.TOKENS;
|
2015-07-10 16:37:13 +03:00
|
|
|
// Multi tokens
|
2017-10-11 19:56:17 +03:00
|
|
|
const MT = linkify.parser.TOKENS;
|
|
|
|
const MultiToken = MT.Base;
|
|
|
|
const S_START = linkify.parser.start;
|
2015-07-10 16:37:13 +03:00
|
|
|
|
2016-09-27 12:38:01 +03:00
|
|
|
if (TT.UNDERSCORE === undefined) {
|
|
|
|
throw new Error("linkify-matrix requires linkifyjs 2.1.1: this version is too old.");
|
|
|
|
}
|
2015-07-10 16:37:13 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const ROOMALIAS = function(value) {
|
2016-09-13 21:36:46 +03:00
|
|
|
MultiToken.call(this, value);
|
|
|
|
this.type = 'roomalias';
|
|
|
|
this.isLink = true;
|
|
|
|
};
|
|
|
|
ROOMALIAS.prototype = new MultiToken();
|
|
|
|
|
2018-06-27 01:36:01 +03:00
|
|
|
const S_HASH = S_START.jump(TT.POUND);
|
2017-10-11 19:56:17 +03:00
|
|
|
const S_HASH_NAME = new linkify.parser.State();
|
|
|
|
const S_HASH_NAME_COLON = new linkify.parser.State();
|
2020-05-14 15:04:30 +03:00
|
|
|
const S_HASH_NAME_COLON_DOMAIN = new linkify.parser.State(ROOMALIAS);
|
2017-10-11 19:56:17 +03:00
|
|
|
const S_HASH_NAME_COLON_DOMAIN_DOT = new linkify.parser.State();
|
|
|
|
const S_ROOMALIAS = new linkify.parser.State(ROOMALIAS);
|
2018-06-27 01:33:35 +03:00
|
|
|
const S_ROOMALIAS_COLON = new linkify.parser.State();
|
2018-06-27 01:41:46 +03:00
|
|
|
const S_ROOMALIAS_COLON_NUM = new linkify.parser.State(ROOMALIAS);
|
2015-07-10 16:37:13 +03:00
|
|
|
|
2019-09-18 11:27:43 +03:00
|
|
|
const roomnameTokens = [
|
2015-07-10 16:37:13 +03:00
|
|
|
TT.DOT,
|
|
|
|
TT.PLUS,
|
|
|
|
TT.NUM,
|
|
|
|
TT.DOMAIN,
|
2016-09-21 16:11:43 +03:00
|
|
|
TT.TLD,
|
|
|
|
TT.UNDERSCORE,
|
|
|
|
TT.POUND,
|
2016-09-23 16:02:14 +03:00
|
|
|
|
|
|
|
// because 'localhost' is tokenised to the localhost token,
|
|
|
|
// usernames @localhost:foo.com are otherwise not matched!
|
|
|
|
TT.LOCALHOST,
|
2015-07-10 16:37:13 +03:00
|
|
|
];
|
|
|
|
|
2019-09-18 11:27:43 +03:00
|
|
|
S_HASH.on(roomnameTokens, S_HASH_NAME);
|
|
|
|
S_HASH_NAME.on(roomnameTokens, S_HASH_NAME);
|
2016-09-13 21:36:46 +03:00
|
|
|
S_HASH_NAME.on(TT.DOMAIN, S_HASH_NAME);
|
2015-07-10 16:37:13 +03:00
|
|
|
|
|
|
|
S_HASH_NAME.on(TT.COLON, S_HASH_NAME_COLON);
|
|
|
|
|
2016-09-13 21:36:46 +03:00
|
|
|
S_HASH_NAME_COLON.on(TT.DOMAIN, S_HASH_NAME_COLON_DOMAIN);
|
2016-09-23 16:02:14 +03:00
|
|
|
S_HASH_NAME_COLON.on(TT.LOCALHOST, S_ROOMALIAS); // accept #foo:localhost
|
2018-06-23 21:42:03 +03:00
|
|
|
S_HASH_NAME_COLON.on(TT.TLD, S_ROOMALIAS); // accept #foo:com (mostly for (TLD|DOMAIN)+ mixing)
|
2016-09-13 21:36:46 +03:00
|
|
|
S_HASH_NAME_COLON_DOMAIN.on(TT.DOT, S_HASH_NAME_COLON_DOMAIN_DOT);
|
|
|
|
S_HASH_NAME_COLON_DOMAIN_DOT.on(TT.DOMAIN, S_HASH_NAME_COLON_DOMAIN);
|
|
|
|
S_HASH_NAME_COLON_DOMAIN_DOT.on(TT.TLD, S_ROOMALIAS);
|
2015-07-10 16:37:13 +03:00
|
|
|
|
2018-06-23 21:28:48 +03:00
|
|
|
S_ROOMALIAS.on(TT.DOT, S_HASH_NAME_COLON_DOMAIN_DOT); // accept repeated TLDs (e.g .org.uk)
|
2018-06-27 01:33:35 +03:00
|
|
|
S_ROOMALIAS.on(TT.COLON, S_ROOMALIAS_COLON); // do not accept trailing `:`
|
2018-06-27 01:41:46 +03:00
|
|
|
S_ROOMALIAS_COLON.on(TT.NUM, S_ROOMALIAS_COLON_NUM); // but do accept :NUM (port specifier)
|
2018-06-23 21:28:48 +03:00
|
|
|
|
2015-07-10 16:37:13 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const USERID = function(value) {
|
2016-09-13 21:36:46 +03:00
|
|
|
MultiToken.call(this, value);
|
|
|
|
this.type = 'userid';
|
|
|
|
this.isLink = true;
|
2015-07-10 16:37:13 +03:00
|
|
|
};
|
2016-09-13 21:36:46 +03:00
|
|
|
USERID.prototype = new MultiToken();
|
2015-07-10 16:37:13 +03:00
|
|
|
|
2018-06-27 01:36:01 +03:00
|
|
|
const S_AT = S_START.jump(TT.AT);
|
2017-10-11 19:56:17 +03:00
|
|
|
const S_AT_NAME = new linkify.parser.State();
|
|
|
|
const S_AT_NAME_COLON = new linkify.parser.State();
|
2020-05-14 15:04:30 +03:00
|
|
|
const S_AT_NAME_COLON_DOMAIN = new linkify.parser.State(USERID);
|
2017-10-11 19:56:17 +03:00
|
|
|
const S_AT_NAME_COLON_DOMAIN_DOT = new linkify.parser.State();
|
|
|
|
const S_USERID = new linkify.parser.State(USERID);
|
2018-06-27 01:33:35 +03:00
|
|
|
const S_USERID_COLON = new linkify.parser.State();
|
2018-06-27 01:41:46 +03:00
|
|
|
const S_USERID_COLON_NUM = new linkify.parser.State(USERID);
|
2015-07-10 16:37:13 +03:00
|
|
|
|
2019-09-18 11:27:43 +03:00
|
|
|
const usernameTokens = [
|
2015-07-10 16:37:13 +03:00
|
|
|
TT.DOT,
|
2016-09-23 16:02:14 +03:00
|
|
|
TT.UNDERSCORE,
|
2015-07-10 16:37:13 +03:00
|
|
|
TT.PLUS,
|
|
|
|
TT.NUM,
|
|
|
|
TT.DOMAIN,
|
2016-09-23 16:02:14 +03:00
|
|
|
TT.TLD,
|
|
|
|
|
2019-09-18 11:27:43 +03:00
|
|
|
// as in roomnameTokens
|
2016-09-23 16:02:14 +03:00
|
|
|
TT.LOCALHOST,
|
2015-07-10 16:37:13 +03:00
|
|
|
];
|
|
|
|
|
2019-09-18 11:27:43 +03:00
|
|
|
S_AT.on(usernameTokens, S_AT_NAME);
|
|
|
|
S_AT_NAME.on(usernameTokens, S_AT_NAME);
|
2016-09-13 21:36:46 +03:00
|
|
|
S_AT_NAME.on(TT.DOMAIN, S_AT_NAME);
|
2015-07-10 16:37:13 +03:00
|
|
|
|
|
|
|
S_AT_NAME.on(TT.COLON, S_AT_NAME_COLON);
|
|
|
|
|
2016-09-13 21:36:46 +03:00
|
|
|
S_AT_NAME_COLON.on(TT.DOMAIN, S_AT_NAME_COLON_DOMAIN);
|
2016-09-23 16:02:14 +03:00
|
|
|
S_AT_NAME_COLON.on(TT.LOCALHOST, S_USERID); // accept @foo:localhost
|
2018-06-23 21:42:03 +03:00
|
|
|
S_AT_NAME_COLON.on(TT.TLD, S_USERID); // accept @foo:com (mostly for (TLD|DOMAIN)+ mixing)
|
2016-09-13 21:36:46 +03:00
|
|
|
S_AT_NAME_COLON_DOMAIN.on(TT.DOT, S_AT_NAME_COLON_DOMAIN_DOT);
|
|
|
|
S_AT_NAME_COLON_DOMAIN_DOT.on(TT.DOMAIN, S_AT_NAME_COLON_DOMAIN);
|
|
|
|
S_AT_NAME_COLON_DOMAIN_DOT.on(TT.TLD, S_USERID);
|
2017-06-05 18:51:50 +03:00
|
|
|
|
2018-06-23 21:28:48 +03:00
|
|
|
S_USERID.on(TT.DOT, S_AT_NAME_COLON_DOMAIN_DOT); // accept repeated TLDs (e.g .org.uk)
|
2018-06-27 01:33:35 +03:00
|
|
|
S_USERID.on(TT.COLON, S_USERID_COLON); // do not accept trailing `:`
|
2018-06-27 01:41:46 +03:00
|
|
|
S_USERID_COLON.on(TT.NUM, S_USERID_COLON_NUM); // but do accept :NUM (port specifier)
|
2018-06-23 21:28:48 +03:00
|
|
|
|
2017-06-05 18:51:50 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const GROUPID = function(value) {
|
2017-06-05 18:51:50 +03:00
|
|
|
MultiToken.call(this, value);
|
|
|
|
this.type = 'groupid';
|
|
|
|
this.isLink = true;
|
|
|
|
};
|
|
|
|
GROUPID.prototype = new MultiToken();
|
|
|
|
|
2018-06-27 01:36:01 +03:00
|
|
|
const S_PLUS = S_START.jump(TT.PLUS);
|
2017-10-11 19:56:17 +03:00
|
|
|
const S_PLUS_NAME = new linkify.parser.State();
|
|
|
|
const S_PLUS_NAME_COLON = new linkify.parser.State();
|
2020-05-14 15:04:30 +03:00
|
|
|
const S_PLUS_NAME_COLON_DOMAIN = new linkify.parser.State(GROUPID);
|
2017-10-11 19:56:17 +03:00
|
|
|
const S_PLUS_NAME_COLON_DOMAIN_DOT = new linkify.parser.State();
|
|
|
|
const S_GROUPID = new linkify.parser.State(GROUPID);
|
2018-06-27 01:33:35 +03:00
|
|
|
const S_GROUPID_COLON = new linkify.parser.State();
|
2018-06-27 01:41:46 +03:00
|
|
|
const S_GROUPID_COLON_NUM = new linkify.parser.State(GROUPID);
|
2017-06-05 18:51:50 +03:00
|
|
|
|
2019-09-18 11:27:43 +03:00
|
|
|
const groupIdTokens = [
|
2017-06-05 18:51:50 +03:00
|
|
|
TT.DOT,
|
|
|
|
TT.UNDERSCORE,
|
|
|
|
TT.PLUS,
|
|
|
|
TT.NUM,
|
|
|
|
TT.DOMAIN,
|
|
|
|
TT.TLD,
|
|
|
|
|
2019-09-18 11:27:43 +03:00
|
|
|
// as in roomnameTokens
|
2017-06-05 18:51:50 +03:00
|
|
|
TT.LOCALHOST,
|
|
|
|
];
|
|
|
|
|
2019-09-18 11:27:43 +03:00
|
|
|
S_PLUS.on(groupIdTokens, S_PLUS_NAME);
|
|
|
|
S_PLUS_NAME.on(groupIdTokens, S_PLUS_NAME);
|
2017-06-05 18:51:50 +03:00
|
|
|
S_PLUS_NAME.on(TT.DOMAIN, S_PLUS_NAME);
|
|
|
|
|
|
|
|
S_PLUS_NAME.on(TT.COLON, S_PLUS_NAME_COLON);
|
|
|
|
|
|
|
|
S_PLUS_NAME_COLON.on(TT.DOMAIN, S_PLUS_NAME_COLON_DOMAIN);
|
|
|
|
S_PLUS_NAME_COLON.on(TT.LOCALHOST, S_GROUPID); // accept +foo:localhost
|
2018-06-23 21:42:03 +03:00
|
|
|
S_PLUS_NAME_COLON.on(TT.TLD, S_GROUPID); // accept +foo:com (mostly for (TLD|DOMAIN)+ mixing)
|
2017-06-05 18:51:50 +03:00
|
|
|
S_PLUS_NAME_COLON_DOMAIN.on(TT.DOT, S_PLUS_NAME_COLON_DOMAIN_DOT);
|
|
|
|
S_PLUS_NAME_COLON_DOMAIN_DOT.on(TT.DOMAIN, S_PLUS_NAME_COLON_DOMAIN);
|
|
|
|
S_PLUS_NAME_COLON_DOMAIN_DOT.on(TT.TLD, S_GROUPID);
|
2018-06-23 21:28:48 +03:00
|
|
|
|
|
|
|
S_GROUPID.on(TT.DOT, S_PLUS_NAME_COLON_DOMAIN_DOT); // accept repeated TLDs (e.g .org.uk)
|
2018-06-27 01:33:35 +03:00
|
|
|
S_GROUPID.on(TT.COLON, S_GROUPID_COLON); // do not accept trailing `:`
|
2018-06-27 01:41:46 +03:00
|
|
|
S_GROUPID_COLON.on(TT.NUM, S_GROUPID_COLON_NUM); // but do accept :NUM (port specifier)
|
2015-07-10 16:37:13 +03:00
|
|
|
}
|
|
|
|
|
2016-08-28 03:55:42 +03:00
|
|
|
// stubs, overwritten in MatrixChat's componentDidMount
|
2016-08-28 04:05:23 +03:00
|
|
|
matrixLinkify.onUserClick = function(e, userId) { e.preventDefault(); };
|
|
|
|
matrixLinkify.onAliasClick = function(e, roomAlias) { e.preventDefault(); };
|
2017-06-05 18:51:50 +03:00
|
|
|
matrixLinkify.onGroupClick = function(e, groupId) { e.preventDefault(); };
|
2015-10-26 20:59:49 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const escapeRegExp = function(string) {
|
2016-03-20 15:31:30 +03:00
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
|
|
};
|
|
|
|
|
2020-12-21 15:46:29 +03:00
|
|
|
// Recognise URLs from both our local and official Element deployments.
|
|
|
|
// Anyone else really should be using matrix.to.
|
|
|
|
matrixLinkify.ELEMENT_URL_PATTERN =
|
|
|
|
"^(?:https?://)?(?:" +
|
|
|
|
escapeRegExp(window.location.host + window.location.pathname) + "|" +
|
2020-12-21 16:15:00 +03:00
|
|
|
"(?:www\\.)?(?:riot|vector)\\.im/(?:app|beta|staging|develop)/|" +
|
|
|
|
"(?:app|beta|staging|develop)\\.element\\.io/" +
|
2020-12-21 15:46:29 +03:00
|
|
|
")(#.*)";
|
2016-08-28 03:55:42 +03:00
|
|
|
|
2019-09-18 11:27:43 +03:00
|
|
|
matrixLinkify.MATRIXTO_URL_PATTERN = "^(?:https?://)?(?:www\\.)?matrix\\.to/#/(([#@!+]).*)";
|
2017-07-21 18:38:31 +03:00
|
|
|
matrixLinkify.MATRIXTO_MD_LINK_PATTERN =
|
2019-09-18 11:27:43 +03:00
|
|
|
'\\[([^\\]]*)\\]\\((?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!+][^\\)]*)\\)';
|
2017-12-10 15:50:41 +03:00
|
|
|
matrixLinkify.MATRIXTO_BASE_URL= baseUrl;
|
2016-03-20 06:05:07 +03:00
|
|
|
|
2015-07-10 16:37:13 +03:00
|
|
|
matrixLinkify.options = {
|
2017-01-20 17:22:27 +03:00
|
|
|
events: function(href, type) {
|
2015-07-10 16:37:13 +03:00
|
|
|
switch (type) {
|
2020-03-20 03:42:44 +03:00
|
|
|
case "url": {
|
|
|
|
// intercept local permalinks to users and show them like userids (in userinfo of current room)
|
2020-03-26 13:45:26 +03:00
|
|
|
try {
|
|
|
|
const permalink = parsePermalink(href);
|
|
|
|
if (permalink && permalink.userId) {
|
|
|
|
return {
|
|
|
|
click: function(e) {
|
|
|
|
matrixLinkify.onUserClick(e, permalink.userId);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// OK fine, it's not actually a permalink
|
2020-03-20 03:42:44 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-10-27 12:58:55 +03:00
|
|
|
case "userid":
|
|
|
|
return {
|
|
|
|
click: function(e) {
|
|
|
|
matrixLinkify.onUserClick(e, href);
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2015-10-27 12:58:55 +03:00
|
|
|
};
|
|
|
|
case "roomalias":
|
|
|
|
return {
|
|
|
|
click: function(e) {
|
|
|
|
matrixLinkify.onAliasClick(e, href);
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2015-10-27 12:58:55 +03:00
|
|
|
};
|
2017-06-05 18:51:50 +03:00
|
|
|
case "groupid":
|
|
|
|
return {
|
|
|
|
click: function(e) {
|
|
|
|
matrixLinkify.onGroupClick(e, href);
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2017-06-05 18:51:50 +03:00
|
|
|
};
|
2015-07-10 16:37:13 +03:00
|
|
|
}
|
2016-02-18 21:16:39 +03:00
|
|
|
},
|
2016-08-28 04:05:23 +03:00
|
|
|
|
2017-01-20 17:22:27 +03:00
|
|
|
formatHref: function(href, type) {
|
2016-02-18 21:16:39 +03:00
|
|
|
switch (type) {
|
2016-03-20 06:05:07 +03:00
|
|
|
case 'roomalias':
|
|
|
|
case 'userid':
|
2017-06-27 15:37:59 +03:00
|
|
|
case 'groupid':
|
2018-06-16 10:27:17 +03:00
|
|
|
default: {
|
2020-03-04 23:56:58 +03:00
|
|
|
return tryTransformEntityToPermalink(href);
|
2018-06-16 10:27:17 +03:00
|
|
|
}
|
2016-03-20 06:05:07 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-15 23:37:26 +03:00
|
|
|
linkAttributes: {
|
2020-02-24 01:14:29 +03:00
|
|
|
rel: 'noreferrer noopener',
|
2016-08-15 23:37:26 +03:00
|
|
|
},
|
|
|
|
|
2016-03-20 06:05:07 +03:00
|
|
|
target: function(href, type) {
|
|
|
|
if (type === 'url') {
|
2019-10-01 05:17:54 +03:00
|
|
|
const transformed = tryTransformPermalinkToLocalHref(href);
|
2020-12-21 15:46:29 +03:00
|
|
|
if (transformed !== href || href.match(matrixLinkify.ELEMENT_URL_PATTERN)) {
|
2016-03-20 06:05:07 +03:00
|
|
|
return null;
|
2017-10-11 19:56:17 +03:00
|
|
|
} else {
|
2016-03-20 06:05:07 +03:00
|
|
|
return '_blank';
|
|
|
|
}
|
|
|
|
}
|
2016-03-21 18:45:04 +03:00
|
|
|
return null;
|
2016-03-20 06:05:07 +03:00
|
|
|
},
|
2015-10-26 20:59:49 +03:00
|
|
|
};
|
2015-07-10 16:37:13 +03:00
|
|
|
|
2019-11-06 12:41:14 +03:00
|
|
|
export default matrixLinkify;
|