2015-07-10 21:37:13 +08:00
|
|
|
/*
|
2016-01-07 12:06:39 +08:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2019-10-01 10:17:54 +08:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2015-07-10 21:37:13 +08: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.
|
|
|
|
*/
|
|
|
|
|
2022-01-21 01:18:47 +08:00
|
|
|
import * as linkifyjs from '@matrix-org/linkifyjs';
|
|
|
|
import linkifyElement from '@matrix-org/linkify-element';
|
|
|
|
import linkifyString from '@matrix-org/linkify-string';
|
2021-12-03 22:00:56 +08:00
|
|
|
import { RoomMember } from 'matrix-js-sdk/src/models/room-member';
|
2022-01-21 01:18:47 +08:00
|
|
|
import { registerCustomProtocol, registerPlugin } from '@matrix-org/linkifyjs';
|
2021-12-09 17:10:23 +08:00
|
|
|
|
2022-01-21 01:18:47 +08:00
|
|
|
import { baseUrl } from "./utils/permalinks/MatrixToPermalinkConstructor";
|
2020-03-20 08:42:44 +08:00
|
|
|
import {
|
|
|
|
parsePermalink,
|
|
|
|
tryTransformEntityToPermalink,
|
2020-03-20 08:56:41 +08:00
|
|
|
tryTransformPermalinkToLocalHref,
|
2020-03-20 08:42:44 +08:00
|
|
|
} from "./utils/permalinks/Permalinks";
|
2021-12-03 22:00:56 +08:00
|
|
|
import dis from './dispatcher/dispatcher';
|
|
|
|
import { Action } from './dispatcher/actions';
|
|
|
|
import { ViewUserPayload } from './dispatcher/payloads/ViewUserPayload';
|
2017-12-10 20:50:41 +08:00
|
|
|
|
2022-01-21 01:18:47 +08:00
|
|
|
export enum Type {
|
2021-09-27 21:24:44 +08:00
|
|
|
URL = "url",
|
|
|
|
UserId = "userid",
|
|
|
|
RoomAlias = "roomalias",
|
|
|
|
GroupId = "groupid"
|
|
|
|
}
|
|
|
|
|
2022-01-19 01:24:16 +08:00
|
|
|
// Linkify stuff doesn't type scanner/parser/utils properly :/
|
|
|
|
function matrixOpaqueIdLinkifyParser({
|
|
|
|
scanner,
|
|
|
|
parser,
|
|
|
|
utils,
|
|
|
|
token,
|
|
|
|
name,
|
|
|
|
}: {
|
|
|
|
scanner: any;
|
|
|
|
parser: any;
|
|
|
|
utils: any;
|
|
|
|
token: '#' | '+' | '@';
|
|
|
|
name: Type;
|
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
DOT,
|
2022-01-21 01:18:47 +08:00
|
|
|
// IPV4 necessity
|
2022-01-19 01:24:16 +08:00
|
|
|
NUM,
|
|
|
|
TLD,
|
|
|
|
COLON,
|
|
|
|
SYM,
|
2022-01-21 01:18:47 +08:00
|
|
|
HYPHEN,
|
2022-01-19 01:24:16 +08:00
|
|
|
UNDERSCORE,
|
|
|
|
// because 'localhost' is tokenised to the localhost token,
|
|
|
|
// usernames @localhost:foo.com are otherwise not matched!
|
|
|
|
LOCALHOST,
|
2022-01-21 01:18:47 +08:00
|
|
|
domain,
|
2022-01-19 01:24:16 +08:00
|
|
|
} = scanner.tokens;
|
2021-12-03 22:00:56 +08:00
|
|
|
|
2022-01-19 01:24:16 +08:00
|
|
|
const S_START = parser.start;
|
|
|
|
const matrixSymbol = utils.createTokenClass(name, { isLink: true });
|
|
|
|
|
2022-01-21 01:18:47 +08:00
|
|
|
const localpartTokens = [domain, TLD, LOCALHOST, SYM, UNDERSCORE, HYPHEN];
|
|
|
|
const domainpartTokens = [domain, TLD, LOCALHOST, HYPHEN];
|
2022-01-19 01:24:16 +08:00
|
|
|
|
|
|
|
const INITIAL_STATE = S_START.tt(token);
|
2015-07-10 21:37:13 +08:00
|
|
|
|
2022-01-21 01:18:47 +08:00
|
|
|
const LOCALPART_STATE = INITIAL_STATE.tt(domain);
|
2022-01-19 01:24:16 +08:00
|
|
|
for (const token of localpartTokens) {
|
|
|
|
INITIAL_STATE.tt(token, LOCALPART_STATE);
|
|
|
|
LOCALPART_STATE.tt(token, LOCALPART_STATE);
|
|
|
|
}
|
|
|
|
const LOCALPART_STATE_DOT = LOCALPART_STATE.tt(DOT);
|
|
|
|
for (const token of localpartTokens) {
|
|
|
|
LOCALPART_STATE_DOT.tt(token, LOCALPART_STATE);
|
|
|
|
}
|
2017-06-05 23:51:50 +08:00
|
|
|
|
2022-01-19 01:24:16 +08:00
|
|
|
const DOMAINPART_STATE_DOT = LOCALPART_STATE.tt(COLON);
|
2022-01-21 01:18:47 +08:00
|
|
|
const DOMAINPART_STATE = DOMAINPART_STATE_DOT.tt(domain);
|
2022-01-19 01:24:16 +08:00
|
|
|
DOMAINPART_STATE.tt(DOT, DOMAINPART_STATE_DOT);
|
|
|
|
for (const token of domainpartTokens) {
|
|
|
|
DOMAINPART_STATE.tt(token, DOMAINPART_STATE);
|
|
|
|
// we are done if we have a domain
|
|
|
|
DOMAINPART_STATE.tt(token, matrixSymbol);
|
|
|
|
}
|
2017-06-05 23:51:50 +08:00
|
|
|
|
2022-01-19 01:24:16 +08:00
|
|
|
// accept repeated TLDs (e.g .org.uk) but do not accept double dots: ..
|
|
|
|
for (const token of domainpartTokens) {
|
|
|
|
DOMAINPART_STATE_DOT.tt(token, DOMAINPART_STATE);
|
|
|
|
}
|
2017-06-05 23:51:50 +08:00
|
|
|
|
2022-01-19 01:24:16 +08:00
|
|
|
const PORT_STATE = DOMAINPART_STATE.tt(COLON);
|
|
|
|
|
|
|
|
PORT_STATE.tt(NUM, matrixSymbol);
|
2015-07-10 21:37:13 +08:00
|
|
|
}
|
|
|
|
|
2021-12-03 22:00:56 +08:00
|
|
|
function onUserClick(event: MouseEvent, userId: string) {
|
2022-01-21 01:18:47 +08:00
|
|
|
event.preventDefault();
|
2021-12-03 22:00:56 +08:00
|
|
|
const member = new RoomMember(null, userId);
|
|
|
|
if (!member) { return; }
|
|
|
|
dis.dispatch<ViewUserPayload>({
|
|
|
|
action: Action.ViewUser,
|
|
|
|
member: member,
|
|
|
|
});
|
|
|
|
}
|
2022-01-21 01:18:47 +08:00
|
|
|
|
2021-12-03 22:00:56 +08:00
|
|
|
function onAliasClick(event: MouseEvent, roomAlias: string) {
|
|
|
|
event.preventDefault();
|
2022-01-13 04:12:28 +08:00
|
|
|
dis.dispatch({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_alias: roomAlias,
|
|
|
|
});
|
2021-12-03 22:00:56 +08:00
|
|
|
}
|
2022-01-21 01:18:47 +08:00
|
|
|
|
2021-12-03 22:00:56 +08:00
|
|
|
function onGroupClick(event: MouseEvent, groupId: string) {
|
|
|
|
event.preventDefault();
|
|
|
|
dis.dispatch({ action: 'view_group', group_id: groupId });
|
|
|
|
}
|
2015-10-27 01:59:49 +08:00
|
|
|
|
2021-09-27 21:24:44 +08:00
|
|
|
const escapeRegExp = function(string): string {
|
2016-03-20 20:31:30 +08:00
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
|
|
};
|
|
|
|
|
2020-12-21 20:46:29 +08:00
|
|
|
// Recognise URLs from both our local and official Element deployments.
|
|
|
|
// Anyone else really should be using matrix.to.
|
2021-12-03 22:00:56 +08:00
|
|
|
export const ELEMENT_URL_PATTERN =
|
2020-12-21 20:46:29 +08:00
|
|
|
"^(?:https?://)?(?:" +
|
|
|
|
escapeRegExp(window.location.host + window.location.pathname) + "|" +
|
2020-12-21 21:15:00 +08:00
|
|
|
"(?:www\\.)?(?:riot|vector)\\.im/(?:app|beta|staging|develop)/|" +
|
|
|
|
"(?:app|beta|staging|develop)\\.element\\.io/" +
|
2020-12-21 20:46:29 +08:00
|
|
|
")(#.*)";
|
2016-08-28 08:55:42 +08:00
|
|
|
|
2021-12-03 22:00:56 +08:00
|
|
|
export const MATRIXTO_URL_PATTERN = "^(?:https?://)?(?:www\\.)?matrix\\.to/#/(([#@!+]).*)";
|
|
|
|
export const MATRIXTO_MD_LINK_PATTERN =
|
2019-09-18 16:27:43 +08:00
|
|
|
'\\[([^\\]]*)\\]\\((?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!+][^\\)]*)\\)';
|
2021-12-03 22:00:56 +08:00
|
|
|
export const MATRIXTO_BASE_URL= baseUrl;
|
2016-03-20 11:05:07 +08:00
|
|
|
|
2021-12-03 22:00:56 +08:00
|
|
|
export const options = {
|
2021-09-27 21:24:44 +08:00
|
|
|
events: function(href: string, type: Type | string): Partial<GlobalEventHandlers> {
|
2015-07-10 21:37:13 +08:00
|
|
|
switch (type) {
|
2021-09-27 21:24:44 +08:00
|
|
|
case Type.URL: {
|
2020-03-20 08:42:44 +08:00
|
|
|
// intercept local permalinks to users and show them like userids (in userinfo of current room)
|
2020-03-26 18:45:26 +08:00
|
|
|
try {
|
|
|
|
const permalink = parsePermalink(href);
|
|
|
|
if (permalink && permalink.userId) {
|
|
|
|
return {
|
2021-09-28 13:53:52 +08:00
|
|
|
// @ts-ignore see https://linkify.js.org/docs/options.html
|
|
|
|
click: function(e) {
|
2021-12-03 22:00:56 +08:00
|
|
|
onUserClick(e, permalink.userId);
|
2020-03-26 18:45:26 +08:00
|
|
|
},
|
|
|
|
};
|
2022-01-21 01:18:47 +08:00
|
|
|
} else {
|
|
|
|
// for events, rooms etc. (anything other then users)
|
|
|
|
const localHref = tryTransformPermalinkToLocalHref(href);
|
|
|
|
if (localHref !== href) {
|
|
|
|
// it could be converted to a localHref -> therefore handle locally
|
|
|
|
return {
|
|
|
|
// @ts-ignore see https://linkify.js.org/docs/options.html
|
|
|
|
click: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
window.location.hash = localHref;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-03-26 18:45:26 +08:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// OK fine, it's not actually a permalink
|
2020-03-20 08:42:44 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-09-27 21:24:44 +08:00
|
|
|
case Type.UserId:
|
2015-10-27 17:58:55 +08:00
|
|
|
return {
|
2021-09-28 13:53:52 +08:00
|
|
|
// @ts-ignore see https://linkify.js.org/docs/options.html
|
|
|
|
click: function(e) {
|
2022-01-21 01:18:47 +08:00
|
|
|
const userId = parsePermalink(href).userId;
|
|
|
|
onUserClick(e, userId);
|
2017-10-12 00:56:17 +08:00
|
|
|
},
|
2015-10-27 17:58:55 +08:00
|
|
|
};
|
2021-09-27 21:24:44 +08:00
|
|
|
case Type.RoomAlias:
|
2015-10-27 17:58:55 +08:00
|
|
|
return {
|
2021-09-28 13:53:52 +08:00
|
|
|
// @ts-ignore see https://linkify.js.org/docs/options.html
|
|
|
|
click: function(e) {
|
2022-01-21 01:18:47 +08:00
|
|
|
const alias = parsePermalink(href).roomIdOrAlias;
|
|
|
|
onAliasClick(e, alias);
|
2017-10-12 00:56:17 +08:00
|
|
|
},
|
2015-10-27 17:58:55 +08:00
|
|
|
};
|
2021-09-27 21:24:44 +08:00
|
|
|
case Type.GroupId:
|
2017-06-05 23:51:50 +08:00
|
|
|
return {
|
2021-09-28 13:53:52 +08:00
|
|
|
// @ts-ignore see https://linkify.js.org/docs/options.html
|
|
|
|
click: function(e) {
|
2022-01-21 01:18:47 +08:00
|
|
|
const groupId = parsePermalink(href).groupId;
|
|
|
|
onGroupClick(e, groupId);
|
2017-10-12 00:56:17 +08:00
|
|
|
},
|
2017-06-05 23:51:50 +08:00
|
|
|
};
|
2015-07-10 21:37:13 +08:00
|
|
|
}
|
2016-02-19 02:16:39 +08:00
|
|
|
},
|
2016-08-28 09:05:23 +08:00
|
|
|
|
2021-09-27 21:24:44 +08:00
|
|
|
formatHref: function(href: string, type: Type | string): string {
|
2016-02-19 02:16:39 +08:00
|
|
|
switch (type) {
|
2021-09-27 21:24:44 +08:00
|
|
|
case Type.RoomAlias:
|
|
|
|
case Type.UserId:
|
|
|
|
case Type.GroupId:
|
2018-06-16 15:27:17 +08:00
|
|
|
default: {
|
2020-03-05 04:56:58 +08:00
|
|
|
return tryTransformEntityToPermalink(href);
|
2018-06-16 15:27:17 +08:00
|
|
|
}
|
2016-03-20 11:05:07 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-01-19 01:24:16 +08:00
|
|
|
attributes: {
|
2020-02-24 06:14:29 +08:00
|
|
|
rel: 'noreferrer noopener',
|
2016-08-16 04:37:26 +08:00
|
|
|
},
|
|
|
|
|
2022-01-19 01:24:16 +08:00
|
|
|
className: 'linkified',
|
|
|
|
|
2021-09-27 21:24:44 +08:00
|
|
|
target: function(href: string, type: Type | string): string {
|
|
|
|
if (type === Type.URL) {
|
2021-05-13 17:20:27 +08:00
|
|
|
try {
|
|
|
|
const transformed = tryTransformPermalinkToLocalHref(href);
|
2022-01-25 22:31:00 +08:00
|
|
|
if (
|
|
|
|
transformed !== href || // if it could be converted to handle locally for matrix symbols e.g. @user:server.tdl and matrix.to
|
|
|
|
decodeURIComponent(href).match(ELEMENT_URL_PATTERN) // for https links to Element domains
|
2022-01-21 01:18:47 +08:00
|
|
|
) {
|
2021-05-13 17:20:27 +08:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return '_blank';
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// malformed URI
|
2016-03-20 11:05:07 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-21 23:45:04 +08:00
|
|
|
return null;
|
2016-03-20 11:05:07 +08:00
|
|
|
},
|
2015-10-27 01:59:49 +08:00
|
|
|
};
|
2015-07-10 21:37:13 +08:00
|
|
|
|
2021-12-03 22:00:56 +08:00
|
|
|
// Run the plugins
|
2022-01-19 01:24:16 +08:00
|
|
|
registerPlugin(Type.RoomAlias, ({ scanner, parser, utils }) => {
|
|
|
|
const token = scanner.tokens.POUND as '#';
|
|
|
|
return matrixOpaqueIdLinkifyParser({
|
|
|
|
scanner,
|
|
|
|
parser,
|
|
|
|
utils,
|
|
|
|
token,
|
|
|
|
name: Type.RoomAlias,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
registerPlugin(Type.GroupId, ({ scanner, parser, utils }) => {
|
|
|
|
const token = scanner.tokens.PLUS as '+';
|
|
|
|
return matrixOpaqueIdLinkifyParser({
|
|
|
|
scanner,
|
|
|
|
parser,
|
|
|
|
utils,
|
|
|
|
token,
|
|
|
|
name: Type.GroupId,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
registerPlugin(Type.UserId, ({ scanner, parser, utils }) => {
|
|
|
|
const token = scanner.tokens.AT as '@';
|
|
|
|
return matrixOpaqueIdLinkifyParser({
|
|
|
|
scanner,
|
|
|
|
parser,
|
|
|
|
utils,
|
|
|
|
token,
|
|
|
|
name: Type.UserId,
|
|
|
|
});
|
|
|
|
});
|
2021-12-03 22:00:56 +08:00
|
|
|
|
2022-01-21 01:18:47 +08:00
|
|
|
registerCustomProtocol("matrix", true);
|
|
|
|
|
2021-12-03 22:00:56 +08:00
|
|
|
export const linkify = linkifyjs;
|
|
|
|
export const _linkifyElement = linkifyElement;
|
|
|
|
export const _linkifyString = linkifyString;
|