From 18a7ca092b1aa1aaa991833c010ef0867cbb35e1 Mon Sep 17 00:00:00 2001 From: Evie Gauthier Date: Wed, 25 Mar 2026 12:13:21 -0400 Subject: [PATCH] fix(abbreviations): highlight terms in HTML-formatted messages Previously, abbreviation term highlighting was skipped for any message that has a formatted_body (virtually all messages from modern clients using a rich-text editor), because RenderBody returned early from the customBody path before reaching the abbreviation logic. The fix adds an optional replaceTextNode callback to getReactCustomHtmlParser. Since opts closes over itself, renderChildren inside element handlers recurses through the same replace function, so text nodes nested inside

, , , list items, etc. are all processed correctly. buildAbbrReplaceTextNode is exported from RenderBody.tsx and wired into both RoomTimeline and ThreadDrawer via useRoomAbbreviationsContext. --- src/app/components/message/RenderBody.tsx | 29 +++++++++++++++++++- src/app/features/room/RoomTimeline.tsx | 6 ++++ src/app/features/room/ThreadDrawer.tsx | 7 ++++- src/app/plugins/react-custom-html-parser.tsx | 5 ++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/app/components/message/RenderBody.tsx b/src/app/components/message/RenderBody.tsx index 679ea7422..c06e86cf8 100644 --- a/src/app/components/message/RenderBody.tsx +++ b/src/app/components/message/RenderBody.tsx @@ -1,4 +1,4 @@ -import { MouseEventHandler, useEffect, useState } from 'react'; +import { MouseEventHandler, ReactNode, useEffect, useState } from 'react'; import parse, { HTMLReactParserOptions } from 'html-react-parser'; import Linkify from 'linkify-react'; import { Opts } from 'linkifyjs'; @@ -59,6 +59,33 @@ function AbbreviationTerm({ text, definition }: AbbreviationTermProps) { ); } +/** + * Builds a `replaceTextNode` callback for use with {@link getReactCustomHtmlParser}. + * Returns `undefined` when there are no abbreviations to apply (avoids creating + * extra closures in the common case). + */ +export function buildAbbrReplaceTextNode( + abbrMap: Map +): ((text: string) => ReactNode | undefined) | undefined { + if (abbrMap.size === 0) return undefined; + return (text: string) => { + const segments = splitByAbbreviations(text, abbrMap); + if (!segments.some((s) => s.termKey !== undefined)) return undefined; + return ( + <> + {segments.map((seg, i) => + seg.termKey !== undefined ? ( + // eslint-disable-next-line react/no-array-index-key + + ) : ( + seg.text + ) + )} + + ); + }; +} + type RenderBodyProps = { body: string; customBody?: string; diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index dd655deec..cd3bda434 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -58,6 +58,8 @@ import { useImagePackRooms } from '$hooks/useImagePackRooms'; import { settingsAtom, MessageLayout } from '$state/settings'; import { useSetting } from '$state/hooks/settings'; import { nicknamesAtom } from '$state/nicknames'; +import { useRoomAbbreviationsContext } from '$hooks/useRoomAbbreviations'; +import { buildAbbrReplaceTextNode } from '$components/message/RenderBody'; import { profilesCacheAtom } from '$state/userRoomProfile'; import { roomToParentsAtom } from '$state/room/roomToParents'; import { roomIdToReplyDraftAtomFamily } from '$state/room/roomInputDrafts'; @@ -451,6 +453,8 @@ export function RoomTimeline({ [mx, room.roomId, mentionClickHandler, nicknames] ); + const abbrMap = useRoomAbbreviationsContext(); + const htmlReactParserOptions = useMemo( () => getReactCustomHtmlParser(mx, room.roomId, { @@ -460,6 +464,7 @@ export function RoomTimeline({ handleMentionClick: mentionClickHandler, nicknames, autoplayEmojis, + replaceTextNode: buildAbbrReplaceTextNode(abbrMap), }), [ mx, @@ -470,6 +475,7 @@ export function RoomTimeline({ nicknames, mediaAuthentication, spoilerClickHandler, + abbrMap, ] ); diff --git a/src/app/features/room/ThreadDrawer.tsx b/src/app/features/room/ThreadDrawer.tsx index 163d40ff0..6a25109fc 100644 --- a/src/app/features/room/ThreadDrawer.tsx +++ b/src/app/features/room/ThreadDrawer.tsx @@ -38,6 +38,8 @@ import { useMediaAuthentication } from '$hooks/useMediaAuthentication'; import { nicknamesAtom } from '$state/nicknames'; import { MessageLayout, MessageSpacing, settingsAtom } from '$state/settings'; import { useSetting } from '$state/hooks/settings'; +import { useRoomAbbreviationsContext } from '$hooks/useRoomAbbreviations'; +import { buildAbbrReplaceTextNode } from '$components/message/RenderBody'; import { createMentionElement, moveCursor, useEditor } from '$components/editor'; import { useMentionClickHandler } from '$hooks/useMentionClickHandler'; import { useSpoilerClickHandler } from '$hooks/useSpoilerClickHandler'; @@ -394,6 +396,8 @@ export function ThreadDrawer({ room, threadRootId, onClose, overlay }: ThreadDra [mx, room, mentionClickHandler, nicknames] ); + const abbrMap = useRoomAbbreviationsContext(); + const htmlReactParserOptions = useMemo( () => getReactCustomHtmlParser(mx, room.roomId, { @@ -402,8 +406,9 @@ export function ThreadDrawer({ room, threadRootId, onClose, overlay }: ThreadDra handleSpoilerClick: spoilerClickHandler, handleMentionClick: mentionClickHandler, nicknames, + replaceTextNode: buildAbbrReplaceTextNode(abbrMap), }), - [mx, room, linkifyOpts, spoilerClickHandler, mentionClickHandler, useAuthentication, nicknames] + [mx, room, linkifyOpts, spoilerClickHandler, mentionClickHandler, useAuthentication, nicknames, abbrMap] ); // Power levels & permissions diff --git a/src/app/plugins/react-custom-html-parser.tsx b/src/app/plugins/react-custom-html-parser.tsx index 1a351572f..8cc101976 100644 --- a/src/app/plugins/react-custom-html-parser.tsx +++ b/src/app/plugins/react-custom-html-parser.tsx @@ -351,10 +351,15 @@ export const getReactCustomHtmlParser = ( useAuthentication?: boolean; nicknames?: Nicknames; autoplayEmojis?: boolean; + replaceTextNode?: (text: string) => ReactNode | undefined; } ): HTMLReactParserOptions => { + const { replaceTextNode } = params; const opts: HTMLReactParserOptions = { replace: (domNode) => { + if (replaceTextNode && domNode instanceof DOMText) { + return replaceTextNode(domNode.data) ?? undefined; + } if (domNode instanceof Element && 'name' in domNode) { const { name, attribs, children, parent } = domNode; const renderChildren = () => domToReact(children as any, opts);