diff --git a/src/app/plugins/matrix-uri.ts b/src/app/plugins/matrix-uri.ts index 0719746df..abce17307 100644 --- a/src/app/plugins/matrix-uri.ts +++ b/src/app/plugins/matrix-uri.ts @@ -88,6 +88,26 @@ export const parseMatrixUri = (href: string): MatrixUriEntity | undefined => { export const testMatrixUri = (href: string): boolean => parseMatrixUri(href) !== undefined; +export const matrixUriKey = (href: string): string | undefined => { + const entity = parseMatrixUri(href); + if (!entity) return undefined; + if (entity.kind === "user") return `u:${entity.userId.toLowerCase()}`; + if (entity.kind === "room") return `r:${entity.room.roomIdOrAlias.toLowerCase()}`; + return `e:${entity.event.roomIdOrAlias.toLowerCase()}/${entity.event.eventId.toLowerCase()}`; +}; + +export const isRedundantMatrixUriAnchorText = ( + href: string, + anchorText: string | undefined, +): boolean => { + if (anchorText == null || anchorText.trim() === "") return true; + const keyHref = matrixUriKey(href); + if (!keyHref) return false; + const keyText = matrixUriKey(anchorText.trim()); + if (!keyText) return false; + return keyHref === keyText; +}; + let matrixProtocolRegistered = false; export const registerMatrixUriProtocol = (): void => { diff --git a/src/app/plugins/react-custom-html-parser.tsx b/src/app/plugins/react-custom-html-parser.tsx index adb78f3a6..ca62195b3 100644 --- a/src/app/plugins/react-custom-html-parser.tsx +++ b/src/app/plugins/react-custom-html-parser.tsx @@ -35,6 +35,7 @@ import { parseMatrixToUser, testMatrixTo, } from './matrix-to'; +import { isRedundantMatrixUriAnchorText, parseMatrixUri, testMatrixUri } from './matrix-uri'; import { getHexcodeForEmoji, getShortcodeFor } from './emoji'; const EMOJI_REG_G = new RegExp(`${URL_NEG_LB}(${EMOJI_PATTERN})`, 'g'); @@ -156,7 +157,10 @@ const matrixPermalinkDisplayLabel = ( ): ReactNode => { if (customChildren === undefined || customChildren === null) return fallback; if (typeof customChildren === 'string') { - return isRedundantMatrixToAnchorText(href, customChildren) ? fallback : customChildren; + const redundant = + isRedundantMatrixToAnchorText(href, customChildren) || + isRedundantMatrixUriAnchorText(href, customChildren); + return redundant ? fallback : customChildren; } return customChildren; }; @@ -168,7 +172,10 @@ export const renderMatrixMention = ( customProps: ComponentPropsWithoutRef<'a'>, nicknames?: Nicknames ) => { - const userId = parseMatrixToUser(href); + const matrixUri = parseMatrixUri(href); + + const userId = + parseMatrixToUser(href) ?? (matrixUri?.kind === 'user' ? matrixUri.userId : undefined); if (userId) { const currentRoom = mx.getRoom(currentRoomId); @@ -187,7 +194,8 @@ export const renderMatrixMention = ( ); } - const matrixToRoom = parseMatrixToRoom(href); + const matrixToRoom = + parseMatrixToRoom(href) ?? (matrixUri?.kind === 'room' ? matrixUri.room : undefined); if (matrixToRoom) { const { roomIdOrAlias, viaServers } = matrixToRoom; const mentionRoom = mx.getRoom( @@ -212,7 +220,8 @@ export const renderMatrixMention = ( ); } - const matrixToRoomEvent = parseMatrixToRoomEvent(href); + const matrixToRoomEvent = + parseMatrixToRoomEvent(href) ?? (matrixUri?.kind === 'event' ? matrixUri.event : undefined); if (matrixToRoomEvent) { const { roomIdOrAlias, eventId, viaServers } = matrixToRoomEvent; const mentionRoom = mx.getRoom( @@ -296,7 +305,7 @@ export const factoryRenderLinkifyWithMention = ( const encodedHref = attributes.href; const decodedHref = encodedHref && safeDecodeUrl(encodedHref); - if (tagName === 'a' && decodedHref && testMatrixTo(decodedHref)) { + if (tagName === 'a' && decodedHref && (testMatrixTo(decodedHref) || testMatrixUri(decodedHref))) { const mention = mentionRender(decodedHref); if (mention) return mention; } @@ -727,7 +736,7 @@ export const getReactCustomHtmlParser = ( ? undefined : children.map((c) => (c instanceof DOMText ? c.data : '')).join(); - if (decodedHref && testMatrixTo(decodedHref)) { + if (decodedHref && (testMatrixTo(decodedHref) || testMatrixUri(decodedHref))) { const mention = renderMatrixMention( mx, roomId,