fix: dynamic matrix.to regexes; route mentions through plugin generators (#67)

- matrix-to.ts: replace static hardcoded regexes with a lazily-built
  cached set that matches both https://matrix.to (cross-client compat)
  and the configured custom base URL (if matrixToBaseUrl is set in
  config.json)
- editor/output.ts: Mention block now calls getMatrixToRoom /
  getMatrixToRoomEvent instead of hardcoding the matrix.to base
- room.ts: parseReplyFormattedBody uses getMatrixToRoomEvent /
  getMatrixToRoom for the same reason
This commit is contained in:
Evie Gauthier committed 2026-03-16 21:17:19 -04:00
1 parent b584d1867b
commit 2da742d716
3 files changed
+44 -26

No files matched your search

+5 -11
View File
@@ -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 `<hr/>`;
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 `<a href="${encodeURI(matrixTo)}">${sanitizeText(node.name)}</a>`;
const href = node.eventId
? getMatrixToRoomEvent(node.id, node.eventId, node.viaServers)
: getMatrixToRoom(node.id, node.viaServers);
return `<a href="${encodeURI(href)}">${sanitizeText(node.name)}</a>`;
}
case BlockType.Emoticon:
return node.key.startsWith('mxc://')
+36 -11
View File
@@ -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];
+3 -4
View File
@@ -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 = `<a href="https://matrix.to/#/${encodeURIComponent(
roomId
)}/${encodeURIComponent(eventId)}">In reply to</a>`;
const userLink = `<a href="https://matrix.to/#/${encodeURIComponent(userId)}">${userId}</a>`;
const replyToLink = `<a href="${getMatrixToRoomEvent(roomId, eventId)}">In reply to</a>`;
const userLink = `<a href="${getMatrixToRoom(userId)}">${userId}</a>`;
return `<mx-reply><blockquote>${replyToLink}${userLink}<br />${formattedBody}</blockquote></mx-reply>`;
};