2015-07-10 15:37:13 +02:00
|
|
|
/*
|
2016-01-07 05:06:39 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2019-10-01 04:17:54 +02:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2015-07-10 15:37:13 +02: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.
|
|
|
|
*/
|
|
|
|
|
2021-12-03 15:00:56 +01:00
|
|
|
import * as linkifyjs from 'linkifyjs';
|
|
|
|
import linkifyElement from 'linkifyjs/element';
|
|
|
|
import linkifyString from 'linkifyjs/string';
|
|
|
|
import { RoomMember } from 'matrix-js-sdk/src/models/room-member';
|
2021-12-09 10:10:23 +01:00
|
|
|
|
2021-06-29 14:11:58 +02:00
|
|
|
import { baseUrl } from "./utils/permalinks/SpecPermalinkConstructor";
|
2020-03-20 01:42:44 +01:00
|
|
|
import {
|
|
|
|
parsePermalink,
|
|
|
|
tryTransformEntityToPermalink,
|
2020-03-20 01:56:41 +01:00
|
|
|
tryTransformPermalinkToLocalHref,
|
2020-03-20 01:42:44 +01:00
|
|
|
} from "./utils/permalinks/Permalinks";
|
2021-12-03 15:00:56 +01:00
|
|
|
import dis from './dispatcher/dispatcher';
|
|
|
|
import { Action } from './dispatcher/actions';
|
|
|
|
import { ViewUserPayload } from './dispatcher/payloads/ViewUserPayload';
|
2017-12-10 13:50:41 +01:00
|
|
|
|
2021-09-27 15:24:44 +02:00
|
|
|
enum Type {
|
|
|
|
URL = "url",
|
|
|
|
UserId = "userid",
|
|
|
|
RoomAlias = "roomalias",
|
|
|
|
GroupId = "groupid"
|
|
|
|
}
|
|
|
|
|
2021-12-03 15:00:56 +01:00
|
|
|
// Linkifyjs types don't have parser, which really makes this harder.
|
|
|
|
const linkifyTokens = (linkifyjs as any).scanner.TOKENS;
|
|
|
|
enum MatrixLinkInitialToken {
|
|
|
|
POUND = linkifyTokens.POUND,
|
|
|
|
PLUS = linkifyTokens.PLUS,
|
|
|
|
AT = linkifyTokens.AT,
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Token should be one of the type of linkify.parser.TOKENS[AT | PLUS | POUND]
|
|
|
|
* but due to typing issues it's just not a feasible solution.
|
|
|
|
* This problem kind of gets solved in linkify 3.0
|
|
|
|
*/
|
|
|
|
function parseFreeformMatrixLinks(linkify, token: MatrixLinkInitialToken, type: Type): void {
|
2015-07-10 15:37:13 +02:00
|
|
|
// Text tokens
|
2017-10-11 18:56:17 +02:00
|
|
|
const TT = linkify.scanner.TOKENS;
|
2015-07-10 15:37:13 +02:00
|
|
|
// Multi tokens
|
2017-10-11 18:56:17 +02:00
|
|
|
const MT = linkify.parser.TOKENS;
|
|
|
|
const MultiToken = MT.Base;
|
|
|
|
const S_START = linkify.parser.start;
|
2015-07-10 15:37:13 +02:00
|
|
|
|
2021-12-03 15:00:56 +01:00
|
|
|
const TOKEN = function(value) {
|
2016-09-13 20:36:46 +02:00
|
|
|
MultiToken.call(this, value);
|
2021-12-03 15:00:56 +01:00
|
|
|
this.type = type;
|
2016-09-13 20:36:46 +02:00
|
|
|
this.isLink = true;
|
|
|
|
};
|
2021-12-03 15:00:56 +01:00
|
|
|
TOKEN.prototype = new MultiToken();
|
|
|
|
|
|
|
|
const S_TOKEN = S_START.jump(token);
|
|
|
|
const S_TOKEN_NAME = new linkify.parser.State();
|
|
|
|
const S_TOKEN_NAME_COLON = new linkify.parser.State();
|
|
|
|
const S_TOKEN_NAME_COLON_DOMAIN = new linkify.parser.State(TOKEN);
|
|
|
|
const S_TOKEN_NAME_COLON_DOMAIN_DOT = new linkify.parser.State();
|
|
|
|
const S_MX_LINK = new linkify.parser.State(TOKEN);
|
|
|
|
const S_MX_LINK_COLON = new linkify.parser.State();
|
|
|
|
const S_MX_LINK_COLON_NUM = new linkify.parser.State(TOKEN);
|
|
|
|
|
|
|
|
const allowedFreeformTokens = [
|
2015-07-10 15:37:13 +02:00
|
|
|
TT.DOT,
|
|
|
|
TT.PLUS,
|
|
|
|
TT.NUM,
|
|
|
|
TT.DOMAIN,
|
2016-09-21 15:11:43 +02:00
|
|
|
TT.TLD,
|
|
|
|
TT.UNDERSCORE,
|
2021-12-03 15:00:56 +01:00
|
|
|
token,
|
2016-09-23 15:02:14 +02:00
|
|
|
|
|
|
|
// because 'localhost' is tokenised to the localhost token,
|
|
|
|
// usernames @localhost:foo.com are otherwise not matched!
|
|
|
|
TT.LOCALHOST,
|
2015-07-10 15:37:13 +02:00
|
|
|
];
|
|
|
|
|
2021-12-03 15:00:56 +01:00
|
|
|
S_TOKEN.on(allowedFreeformTokens, S_TOKEN_NAME);
|
|
|
|
S_TOKEN_NAME.on(allowedFreeformTokens, S_TOKEN_NAME);
|
|
|
|
S_TOKEN_NAME.on(TT.DOMAIN, S_TOKEN_NAME);
|
2017-06-05 17:51:50 +02:00
|
|
|
|
2021-12-03 15:00:56 +01:00
|
|
|
S_TOKEN_NAME.on(TT.COLON, S_TOKEN_NAME_COLON);
|
2017-06-05 17:51:50 +02:00
|
|
|
|
2021-12-03 15:00:56 +01:00
|
|
|
S_TOKEN_NAME_COLON.on(TT.DOMAIN, S_TOKEN_NAME_COLON_DOMAIN);
|
|
|
|
S_TOKEN_NAME_COLON.on(TT.LOCALHOST, S_MX_LINK); // accept #foo:localhost
|
|
|
|
S_TOKEN_NAME_COLON.on(TT.TLD, S_MX_LINK); // accept #foo:com (mostly for (TLD|DOMAIN)+ mixing)
|
|
|
|
S_TOKEN_NAME_COLON_DOMAIN.on(TT.DOT, S_TOKEN_NAME_COLON_DOMAIN_DOT);
|
|
|
|
S_TOKEN_NAME_COLON_DOMAIN_DOT.on(TT.DOMAIN, S_TOKEN_NAME_COLON_DOMAIN);
|
|
|
|
S_TOKEN_NAME_COLON_DOMAIN_DOT.on(TT.TLD, S_MX_LINK);
|
2017-06-05 17:51:50 +02:00
|
|
|
|
2021-12-03 15:00:56 +01:00
|
|
|
S_MX_LINK.on(TT.DOT, S_TOKEN_NAME_COLON_DOMAIN_DOT); // accept repeated TLDs (e.g .org.uk)
|
|
|
|
S_MX_LINK.on(TT.COLON, S_MX_LINK_COLON); // do not accept trailing `:`
|
|
|
|
S_MX_LINK_COLON.on(TT.NUM, S_MX_LINK_COLON_NUM); // but do accept :NUM (port specifier)
|
2015-07-10 15:37:13 +02:00
|
|
|
}
|
|
|
|
|
2021-12-03 15:00:56 +01:00
|
|
|
function onUserClick(event: MouseEvent, userId: string) {
|
|
|
|
const member = new RoomMember(null, userId);
|
|
|
|
if (!member) { return; }
|
|
|
|
dis.dispatch<ViewUserPayload>({
|
|
|
|
action: Action.ViewUser,
|
|
|
|
member: member,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function onAliasClick(event: MouseEvent, roomAlias: string) {
|
|
|
|
event.preventDefault();
|
2022-01-12 21:12:28 +01:00
|
|
|
dis.dispatch({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_alias: roomAlias,
|
|
|
|
});
|
2021-12-03 15:00:56 +01:00
|
|
|
}
|
|
|
|
function onGroupClick(event: MouseEvent, groupId: string) {
|
|
|
|
event.preventDefault();
|
|
|
|
dis.dispatch({ action: 'view_group', group_id: groupId });
|
|
|
|
}
|
2015-10-26 18:59:49 +01:00
|
|
|
|
2021-09-27 15:24:44 +02:00
|
|
|
const escapeRegExp = function(string): string {
|
2016-03-20 13:31:30 +01:00
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
|
|
};
|
|
|
|
|
2020-12-21 13:46:29 +01:00
|
|
|
// Recognise URLs from both our local and official Element deployments.
|
|
|
|
// Anyone else really should be using matrix.to.
|
2021-12-03 15:00:56 +01:00
|
|
|
export const ELEMENT_URL_PATTERN =
|
2020-12-21 13:46:29 +01:00
|
|
|
"^(?:https?://)?(?:" +
|
|
|
|
escapeRegExp(window.location.host + window.location.pathname) + "|" +
|
2020-12-21 14:15:00 +01:00
|
|
|
"(?:www\\.)?(?:riot|vector)\\.im/(?:app|beta|staging|develop)/|" +
|
|
|
|
"(?:app|beta|staging|develop)\\.element\\.io/" +
|
2020-12-21 13:46:29 +01:00
|
|
|
")(#.*)";
|
2016-08-28 02:55:42 +02:00
|
|
|
|
2021-12-03 15:00:56 +01:00
|
|
|
export const MATRIXTO_URL_PATTERN = "^(?:https?://)?(?:www\\.)?matrix\\.to/#/(([#@!+]).*)";
|
|
|
|
export const MATRIXTO_MD_LINK_PATTERN =
|
2019-09-18 10:27:43 +02:00
|
|
|
'\\[([^\\]]*)\\]\\((?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!+][^\\)]*)\\)';
|
2021-12-03 15:00:56 +01:00
|
|
|
export const MATRIXTO_BASE_URL= baseUrl;
|
2016-03-20 04:05:07 +01:00
|
|
|
|
2021-12-03 15:00:56 +01:00
|
|
|
export const options = {
|
2021-09-27 15:24:44 +02:00
|
|
|
events: function(href: string, type: Type | string): Partial<GlobalEventHandlers> {
|
2015-07-10 15:37:13 +02:00
|
|
|
switch (type) {
|
2021-09-27 15:24:44 +02:00
|
|
|
case Type.URL: {
|
2020-03-20 01:42:44 +01:00
|
|
|
// intercept local permalinks to users and show them like userids (in userinfo of current room)
|
2020-03-26 11:45:26 +01:00
|
|
|
try {
|
|
|
|
const permalink = parsePermalink(href);
|
|
|
|
if (permalink && permalink.userId) {
|
|
|
|
return {
|
2021-09-28 07:53:52 +02:00
|
|
|
// @ts-ignore see https://linkify.js.org/docs/options.html
|
|
|
|
click: function(e) {
|
2021-12-03 15:00:56 +01:00
|
|
|
onUserClick(e, permalink.userId);
|
2020-03-26 11:45:26 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// OK fine, it's not actually a permalink
|
2020-03-20 01:42:44 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-09-27 15:24:44 +02:00
|
|
|
case Type.UserId:
|
2015-10-27 10:58:55 +01:00
|
|
|
return {
|
2021-09-28 07:53:52 +02:00
|
|
|
// @ts-ignore see https://linkify.js.org/docs/options.html
|
|
|
|
click: function(e) {
|
2021-12-03 15:00:56 +01:00
|
|
|
onUserClick(e, href);
|
2017-10-11 18:56:17 +02:00
|
|
|
},
|
2015-10-27 10:58:55 +01:00
|
|
|
};
|
2021-09-27 15:24:44 +02:00
|
|
|
case Type.RoomAlias:
|
2015-10-27 10:58:55 +01:00
|
|
|
return {
|
2021-09-28 07:53:52 +02:00
|
|
|
// @ts-ignore see https://linkify.js.org/docs/options.html
|
|
|
|
click: function(e) {
|
2021-12-03 15:00:56 +01:00
|
|
|
onAliasClick(e, href);
|
2017-10-11 18:56:17 +02:00
|
|
|
},
|
2015-10-27 10:58:55 +01:00
|
|
|
};
|
2021-09-27 15:24:44 +02:00
|
|
|
case Type.GroupId:
|
2017-06-05 17:51:50 +02:00
|
|
|
return {
|
2021-09-28 07:53:52 +02:00
|
|
|
// @ts-ignore see https://linkify.js.org/docs/options.html
|
|
|
|
click: function(e) {
|
2021-12-03 15:00:56 +01:00
|
|
|
onGroupClick(e, href);
|
2017-10-11 18:56:17 +02:00
|
|
|
},
|
2017-06-05 17:51:50 +02:00
|
|
|
};
|
2015-07-10 15:37:13 +02:00
|
|
|
}
|
2016-02-18 19:16:39 +01:00
|
|
|
},
|
2016-08-28 03:05:23 +02:00
|
|
|
|
2021-09-27 15:24:44 +02:00
|
|
|
formatHref: function(href: string, type: Type | string): string {
|
2016-02-18 19:16:39 +01:00
|
|
|
switch (type) {
|
2021-09-27 15:24:44 +02:00
|
|
|
case Type.RoomAlias:
|
|
|
|
case Type.UserId:
|
|
|
|
case Type.GroupId:
|
2018-06-16 09:27:17 +02:00
|
|
|
default: {
|
2020-03-04 21:56:58 +01:00
|
|
|
return tryTransformEntityToPermalink(href);
|
2018-06-16 09:27:17 +02:00
|
|
|
}
|
2016-03-20 04:05:07 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-15 22:37:26 +02:00
|
|
|
linkAttributes: {
|
2020-02-23 23:14:29 +01:00
|
|
|
rel: 'noreferrer noopener',
|
2016-08-15 22:37:26 +02:00
|
|
|
},
|
|
|
|
|
2021-09-27 15:24:44 +02:00
|
|
|
target: function(href: string, type: Type | string): string {
|
|
|
|
if (type === Type.URL) {
|
2021-05-13 11:20:27 +02:00
|
|
|
try {
|
|
|
|
const transformed = tryTransformPermalinkToLocalHref(href);
|
2021-12-03 15:00:56 +01:00
|
|
|
if (transformed !== href || decodeURIComponent(href).match(ELEMENT_URL_PATTERN)) {
|
2021-05-13 11:20:27 +02:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return '_blank';
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// malformed URI
|
2016-03-20 04:05:07 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-21 16:45:04 +01:00
|
|
|
return null;
|
2016-03-20 04:05:07 +01:00
|
|
|
},
|
2015-10-26 18:59:49 +01:00
|
|
|
};
|
2015-07-10 15:37:13 +02:00
|
|
|
|
2021-12-03 15:00:56 +01:00
|
|
|
// Run the plugins
|
|
|
|
// Linkify room aliases
|
|
|
|
parseFreeformMatrixLinks(linkifyjs, MatrixLinkInitialToken.POUND, Type.RoomAlias);
|
|
|
|
// Linkify group IDs
|
|
|
|
parseFreeformMatrixLinks(linkifyjs, MatrixLinkInitialToken.PLUS, Type.GroupId);
|
|
|
|
// Linkify user IDs
|
|
|
|
parseFreeformMatrixLinks(linkifyjs, MatrixLinkInitialToken.AT, Type.UserId);
|
|
|
|
|
|
|
|
export const linkify = linkifyjs;
|
|
|
|
export const _linkifyElement = linkifyElement;
|
|
|
|
export const _linkifyString = linkifyString;
|