diff --git a/src/app/components/editor/output.ts b/src/app/components/editor/output.ts index 9e390192..fa9a4f06 100644 --- a/src/app/components/editor/output.ts +++ b/src/app/components/editor/output.ts @@ -10,6 +10,7 @@ import { import { findAndReplace } from '$utils/findAndReplace'; import { sanitizeForRegex } from '$utils/regex'; import { isUserId } from '$utils/matrix'; +import { getMatrixToRoom, getMatrixToRoomEvent } from '$plugins/matrix-to'; import { CustomElement } from './slate'; import { BlockType } from './types'; @@ -63,17 +64,10 @@ const elementToCustomHtml = (node: CustomElement, children: string): string => { return `
`; case BlockType.Mention: { - let fragment = node.id; - - if (node.eventId) { - fragment += `/${node.eventId}`; - } - if (node.viaServers && node.viaServers.length > 0) { - fragment += `?${node.viaServers.map((server) => `via=${server}`).join('&')}`; - } - - const matrixTo = `https://matrix.to/#/${fragment}`; - return `${sanitizeText(node.name)}`; + const href = node.eventId + ? getMatrixToRoomEvent(node.id, node.eventId, node.viaServers) + : getMatrixToRoom(node.id, node.viaServers); + return `${sanitizeText(node.name)}`; } case BlockType.Emoticon: return node.key.startsWith('mxc://') diff --git a/src/app/plugins/matrix-to.ts b/src/app/plugins/matrix-to.ts index 35afe11d..ae0a9f4f 100644 --- a/src/app/plugins/matrix-to.ts +++ b/src/app/plugins/matrix-to.ts @@ -46,23 +46,48 @@ export type MatrixToRoomEvent = MatrixToRoom & { eventId: string; }; -const MATRIX_TO = /^https?:\/\/matrix\.to\S*$/; -export const testMatrixTo = (href: string): boolean => MATRIX_TO.test(href); +const escapeForRegex = (str: string): string => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); -const MATRIX_TO_USER = /^https?:\/\/matrix\.to\/#\/(@[^:\s]+:[^?/\s]+)\/?$/; -const MATRIX_TO_ROOM = /^https?:\/\/matrix\.to\/#\/([#!][^?/\s]+)\/?(\?[\S]*)?$/; -const MATRIX_TO_ROOM_EVENT = - /^https?:\/\/matrix\.to\/#\/([#!][^?/\s]+)\/(\$[^?/\s]+)\/?(\?[\S]*)?$/; +// Lazily cached regex set; rebuilt if MATRIX_TO_BASE changes. +let cachedRegexBase = ''; +let cachedRegexes: { + any: RegExp; + user: RegExp; + room: RegExp; + event: RegExp; +} | null = null; + +/** + * Returns regexes that match BOTH https://matrix.to (for cross-client links + * received from standard clients) and the configured custom base URL (if any). + */ +const getMatchRegexes = () => { + if (cachedRegexBase === MATRIX_TO_BASE && cachedRegexes) return cachedRegexes; + cachedRegexBase = MATRIX_TO_BASE; + const standard = escapeForRegex('https://matrix.to'); + const b = + MATRIX_TO_BASE !== 'https://matrix.to' + ? `(?:${standard}|${escapeForRegex(MATRIX_TO_BASE)})` + : standard; + cachedRegexes = { + any: new RegExp(`^${b}\\S*$`), + user: new RegExp(`^${b}/#/(@[^:\\s]+:[^?/\\s]+)\\/?$`), + room: new RegExp(`^${b}/#/([#!][^?/\\s]+)\\/?([?\\S]*)?$`), + event: new RegExp(`^${b}/#/([#!][^?/\\s]+)/(\\$[^?/\\s]+)\\/?([?\\S]*)?$`), + }; + return cachedRegexes; +}; + +export const testMatrixTo = (href: string): boolean => getMatchRegexes().any.test(href); export const parseMatrixToUser = (href: string): string | undefined => { - const match = href.match(MATRIX_TO_USER); + const match = href.match(getMatchRegexes().user); if (!match) return undefined; - const userId = match[1]; - return userId; + return match[1]; }; export const parseMatrixToRoom = (href: string): MatrixToRoom | undefined => { - const match = href.match(MATRIX_TO_ROOM); + const match = href.match(getMatchRegexes().room); if (!match) return undefined; const roomIdOrAlias = match[1]; @@ -76,7 +101,7 @@ export const parseMatrixToRoom = (href: string): MatrixToRoom | undefined => { }; export const parseMatrixToRoomEvent = (href: string): MatrixToRoomEvent | undefined => { - const match = href.match(MATRIX_TO_ROOM_EVENT); + const match = href.match(getMatchRegexes().event); if (!match) return undefined; const roomIdOrAlias = match[1]; diff --git a/src/app/utils/room.ts b/src/app/utils/room.ts index 5003d53d..2c30d1a7 100644 --- a/src/app/utils/room.ts +++ b/src/app/utils/room.ts @@ -1,4 +1,5 @@ import { IconName, IconSrc } from 'folds'; +import { getMatrixToRoom, getMatrixToRoomEvent } from '$plugins/matrix-to'; import { EventTimeline, @@ -511,10 +512,8 @@ export const parseReplyFormattedBody = ( eventId: string, formattedBody: string ): string => { - const replyToLink = `In reply to`; - const userLink = `${userId}`; + const replyToLink = `In reply to`; + const userLink = `${userId}`; return `
${replyToLink}${userLink}
${formattedBody}
`; };