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.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import * as linkifyjs from "linkifyjs";
|
2023-04-25 10:28:48 +02:00
|
|
|
import { EventListeners, Opts, registerCustomProtocol, registerPlugin } from "linkifyjs";
|
2022-12-12 12:24:14 +01:00
|
|
|
import linkifyElement from "linkify-element";
|
|
|
|
import linkifyString from "linkify-string";
|
2023-02-16 18:21:44 +01:00
|
|
|
import { User } from "matrix-js-sdk/src/matrix";
|
2021-12-09 10:10:23 +01:00
|
|
|
|
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";
|
2022-12-12 12:24:14 +01:00
|
|
|
import dis from "./dispatcher/dispatcher";
|
|
|
|
import { Action } from "./dispatcher/actions";
|
|
|
|
import { ViewUserPayload } from "./dispatcher/payloads/ViewUserPayload";
|
2022-02-10 15:29:55 +01:00
|
|
|
import { ViewRoomPayload } from "./dispatcher/payloads/ViewRoomPayload";
|
2017-12-10 13:50:41 +01:00
|
|
|
|
2022-01-20 18:18:47 +01:00
|
|
|
export enum Type {
|
2021-09-27 15:24:44 +02:00
|
|
|
URL = "url",
|
|
|
|
UserId = "userid",
|
|
|
|
RoomAlias = "roomalias",
|
|
|
|
}
|
|
|
|
|
2022-01-18 18:24:16 +01:00
|
|
|
// Linkify stuff doesn't type scanner/parser/utils properly :/
|
|
|
|
function matrixOpaqueIdLinkifyParser({
|
|
|
|
scanner,
|
|
|
|
parser,
|
|
|
|
utils,
|
|
|
|
token,
|
|
|
|
name,
|
|
|
|
}: {
|
|
|
|
scanner: any;
|
|
|
|
parser: any;
|
|
|
|
utils: any;
|
2022-12-12 12:24:14 +01:00
|
|
|
token: "#" | "+" | "@";
|
2022-01-18 18:24:16 +01:00
|
|
|
name: Type;
|
2023-01-12 14:25:14 +01:00
|
|
|
}): void {
|
2022-01-18 18:24:16 +01:00
|
|
|
const {
|
|
|
|
DOT,
|
2022-01-20 18:18:47 +01:00
|
|
|
// IPV4 necessity
|
2022-01-18 18:24:16 +01:00
|
|
|
NUM,
|
|
|
|
TLD,
|
|
|
|
COLON,
|
|
|
|
SYM,
|
2022-01-20 18:18:47 +01:00
|
|
|
HYPHEN,
|
2022-01-18 18:24:16 +01:00
|
|
|
UNDERSCORE,
|
|
|
|
// because 'localhost' is tokenised to the localhost token,
|
|
|
|
// usernames @localhost:foo.com are otherwise not matched!
|
|
|
|
LOCALHOST,
|
2022-01-20 18:18:47 +01:00
|
|
|
domain,
|
2022-01-18 18:24:16 +01:00
|
|
|
} = scanner.tokens;
|
2021-12-03 15:00:56 +01:00
|
|
|
|
2022-01-18 18:24:16 +01:00
|
|
|
const S_START = parser.start;
|
|
|
|
const matrixSymbol = utils.createTokenClass(name, { isLink: true });
|
|
|
|
|
2022-01-20 18:18:47 +01:00
|
|
|
const localpartTokens = [domain, TLD, LOCALHOST, SYM, UNDERSCORE, HYPHEN];
|
|
|
|
const domainpartTokens = [domain, TLD, LOCALHOST, HYPHEN];
|
2022-01-18 18:24:16 +01:00
|
|
|
|
|
|
|
const INITIAL_STATE = S_START.tt(token);
|
2015-07-10 15:37:13 +02:00
|
|
|
|
2022-01-20 18:18:47 +01:00
|
|
|
const LOCALPART_STATE = INITIAL_STATE.tt(domain);
|
2022-01-18 18:24:16 +01: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 17:51:50 +02:00
|
|
|
|
2022-01-18 18:24:16 +01:00
|
|
|
const DOMAINPART_STATE_DOT = LOCALPART_STATE.tt(COLON);
|
2022-01-20 18:18:47 +01:00
|
|
|
const DOMAINPART_STATE = DOMAINPART_STATE_DOT.tt(domain);
|
2022-01-18 18:24:16 +01: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 17:51:50 +02:00
|
|
|
|
2022-01-18 18:24:16 +01: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 17:51:50 +02:00
|
|
|
|
2022-01-18 18:24:16 +01:00
|
|
|
const PORT_STATE = DOMAINPART_STATE.tt(COLON);
|
|
|
|
|
|
|
|
PORT_STATE.tt(NUM, matrixSymbol);
|
2015-07-10 15:37:13 +02:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
function onUserClick(event: MouseEvent, userId: string): void {
|
2022-01-20 18:18:47 +01:00
|
|
|
event.preventDefault();
|
2021-12-03 15:00:56 +01:00
|
|
|
dis.dispatch<ViewUserPayload>({
|
|
|
|
action: Action.ViewUser,
|
2023-02-16 18:21:44 +01:00
|
|
|
member: new User(userId),
|
2021-12-03 15:00:56 +01:00
|
|
|
});
|
|
|
|
}
|
2022-01-20 18:18:47 +01:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
function onAliasClick(event: MouseEvent, roomAlias: string): void {
|
2021-12-03 15:00:56 +01:00
|
|
|
event.preventDefault();
|
2022-02-10 15:29:55 +01:00
|
|
|
dis.dispatch<ViewRoomPayload>({
|
2022-01-12 21:12:28 +01:00
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_alias: roomAlias,
|
2022-02-17 19:03:27 +01:00
|
|
|
metricsTrigger: "Timeline",
|
|
|
|
metricsViaKeyboard: false,
|
2022-01-12 21:12:28 +01:00
|
|
|
});
|
2021-12-03 15:00:56 +01:00
|
|
|
}
|
2022-01-20 18:18:47 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const escapeRegExp = function (s: string): string {
|
2022-08-09 13:55:49 +02:00
|
|
|
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
2016-03-20 13:31:30 +01:00
|
|
|
};
|
|
|
|
|
2020-12-21 13:46:29 +01:00
|
|
|
// Recognise URLs from both our local and official Element deployments.
|
2022-02-03 09:55:49 +01:00
|
|
|
// Anyone else really should be using matrix.to. vector:// allowed to support Element Desktop relative links.
|
2021-12-03 15:00:56 +01:00
|
|
|
export const ELEMENT_URL_PATTERN =
|
2022-02-03 09:55:49 +01:00
|
|
|
"^(?:vector://|https?://)?(?:" +
|
2022-12-12 12:24:14 +01:00
|
|
|
escapeRegExp(window.location.host + window.location.pathname) +
|
|
|
|
"|" +
|
|
|
|
"(?: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
|
|
|
|
2023-02-03 09:59:21 +01:00
|
|
|
export const options: Opts = {
|
2023-04-25 10:28:48 +02:00
|
|
|
events: function (href: string, type: string): EventListeners {
|
2023-02-03 09:59:21 +01:00
|
|
|
switch (type as 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);
|
2022-03-23 14:38:57 +01:00
|
|
|
if (permalink?.userId) {
|
2020-03-26 11:45:26 +01:00
|
|
|
return {
|
2021-09-28 07:53:52 +02:00
|
|
|
// @ts-ignore see https://linkify.js.org/docs/options.html
|
2022-12-12 12:24:14 +01:00
|
|
|
click: function (e: MouseEvent) {
|
2021-12-03 15:00:56 +01:00
|
|
|
onUserClick(e, permalink.userId);
|
2020-03-26 11:45:26 +01:00
|
|
|
},
|
|
|
|
};
|
2022-01-20 18:18:47 +01:00
|
|
|
} else {
|
2022-03-23 14:38:57 +01:00
|
|
|
// for events, rooms etc. (anything other than users)
|
2022-01-20 18:18:47 +01:00
|
|
|
const localHref = tryTransformPermalinkToLocalHref(href);
|
|
|
|
if (localHref !== href) {
|
|
|
|
// it could be converted to a localHref -> therefore handle locally
|
|
|
|
return {
|
2022-12-12 12:24:14 +01:00
|
|
|
// @ts-ignore see https://linkify.js.org/docs/options.html
|
|
|
|
click: function (e: MouseEvent) {
|
2022-01-20 18:18:47 +01:00
|
|
|
e.preventDefault();
|
|
|
|
window.location.hash = localHref;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
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
|
2022-12-12 12:24:14 +01:00
|
|
|
click: function (e: MouseEvent) {
|
2023-02-24 16:28:40 +01:00
|
|
|
const userId = parsePermalink(href)?.userId;
|
|
|
|
if (userId) onUserClick(e, userId);
|
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
|
2022-12-12 12:24:14 +01:00
|
|
|
click: function (e: MouseEvent) {
|
2023-02-24 16:28:40 +01:00
|
|
|
const alias = parsePermalink(href)?.roomIdOrAlias;
|
|
|
|
if (alias) onAliasClick(e, alias);
|
2017-10-11 18:56:17 +02:00
|
|
|
},
|
2015-10-27 10:58:55 +01:00
|
|
|
};
|
2015-07-10 15:37:13 +02:00
|
|
|
}
|
2023-04-25 10:28:48 +02:00
|
|
|
|
|
|
|
return {};
|
2016-02-18 19:16:39 +01:00
|
|
|
},
|
2016-08-28 03:05:23 +02:00
|
|
|
|
2023-04-25 10:28:48 +02:00
|
|
|
formatHref: function (href: string, type: Type | string): string | null {
|
2016-02-18 19:16:39 +01:00
|
|
|
switch (type) {
|
2021-09-27 15:24:44 +02:00
|
|
|
case Type.RoomAlias:
|
|
|
|
case Type.UserId:
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-01-18 18:24:16 +01:00
|
|
|
attributes: {
|
2022-12-12 12:24:14 +01:00
|
|
|
rel: "noreferrer noopener",
|
2016-08-15 22:37:26 +02:00
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
ignoreTags: ["pre", "code"],
|
2022-05-03 02:26:37 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
className: "linkified",
|
2022-01-18 18:24:16 +01:00
|
|
|
|
2023-04-25 10:28:48 +02:00
|
|
|
target: function (href: string, type: Type | string): string | null {
|
2021-09-27 15:24:44 +02:00
|
|
|
if (type === Type.URL) {
|
2021-05-13 11:20:27 +02:00
|
|
|
try {
|
|
|
|
const transformed = tryTransformPermalinkToLocalHref(href);
|
2022-01-25 15:31:00 +01: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-20 18:18:47 +01:00
|
|
|
) {
|
2021-05-13 11:20:27 +02:00
|
|
|
return null;
|
|
|
|
} else {
|
2022-12-12 12:24:14 +01:00
|
|
|
return "_blank";
|
2021-05-13 11:20:27 +02:00
|
|
|
}
|
|
|
|
} 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
|
2023-02-13 12:39:16 +01:00
|
|
|
registerPlugin(Type.RoomAlias, ({ scanner, parser, utils }: any) => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const token = scanner.tokens.POUND as "#";
|
2022-01-25 18:37:54 +01:00
|
|
|
matrixOpaqueIdLinkifyParser({
|
2022-01-18 18:24:16 +01:00
|
|
|
scanner,
|
|
|
|
parser,
|
|
|
|
utils,
|
|
|
|
token,
|
|
|
|
name: Type.RoomAlias,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
registerPlugin(Type.UserId, ({ scanner, parser, utils }: any) => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const token = scanner.tokens.AT as "@";
|
2022-01-25 18:37:54 +01:00
|
|
|
matrixOpaqueIdLinkifyParser({
|
2022-01-18 18:24:16 +01:00
|
|
|
scanner,
|
|
|
|
parser,
|
|
|
|
utils,
|
|
|
|
token,
|
|
|
|
name: Type.UserId,
|
|
|
|
});
|
|
|
|
});
|
2021-12-03 15:00:56 +01:00
|
|
|
|
2022-01-20 18:18:47 +01:00
|
|
|
registerCustomProtocol("matrix", true);
|
|
|
|
|
2021-12-03 15:00:56 +01:00
|
|
|
export const linkify = linkifyjs;
|
|
|
|
export const _linkifyElement = linkifyElement;
|
|
|
|
export const _linkifyString = linkifyString;
|