mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 09:01:17 -08:00
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 <p>, <strong>, <em>, list items, etc. are all processed correctly. buildAbbrReplaceTextNode is exported from RenderBody.tsx and wired into both RoomTimeline and ThreadDrawer via useRoomAbbreviationsContext.
This commit is contained in:
4 files changed
+45
-2
No files matched your search
@@ -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<string, string>
|
||||
): ((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
|
||||
<AbbreviationTerm key={i} text={seg.text} definition={abbrMap.get(seg.termKey) ?? ''} />
|
||||
) : (
|
||||
seg.text
|
||||
)
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
type RenderBodyProps = {
|
||||
body: string;
|
||||
customBody?: string;
|
||||
|
||||
@@ -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,
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
@@ -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<HTMLReactParserOptions>(
|
||||
() =>
|
||||
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
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user