mirror of
https://github.com/Novaloop-AG/healthchat-web.git
synced 2026-11-24 17:48:48 +00:00
Fix highlights in messages (or search results) breaking links (#30264)
* Fix highlights in messages (or search results) breaking links Fixes #17011 and fixes #29807, by running the linkifier that turns text into links before the highlighter that adds highlights to text. * Fix jest test * Fix tests related to emojis and pills-inside-spoilers * Remove dead code * Address review comments around sanitizeParams * Address review comment about linkify-matrix * Fix code style * Refactor if statement per review --------- Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
@@ -9,10 +9,4 @@ export { ambiguousLinkTooltipRenderer } from "./link-tooltip";
|
||||
export { keywordPillRenderer, mentionPillRenderer } from "./pill";
|
||||
export { spoilerRenderer } from "./spoiler";
|
||||
export { codeBlockRenderer } from "./code-block";
|
||||
export {
|
||||
applyReplacerOnString,
|
||||
replacerToRenderFunction,
|
||||
combineRenderers,
|
||||
type RendererMap,
|
||||
type Replacer,
|
||||
} from "./utils";
|
||||
export { applyReplacerOnString, combineRenderers, type RendererMap, type Replacer } from "./utils";
|
||||
|
||||
@@ -15,10 +15,12 @@ import Spoiler from "../components/views/elements/Spoiler.tsx";
|
||||
* Replaces spans with `data-mx-spoiler` with a Spoiler component.
|
||||
*/
|
||||
export const spoilerRenderer: RendererMap = {
|
||||
span: (span) => {
|
||||
span: (span, params) => {
|
||||
const reason = span.attribs["data-mx-spoiler"];
|
||||
if (typeof reason === "string") {
|
||||
return <Spoiler reason={reason}>{domToReact(span.children as DOMNode[])}</Spoiler>;
|
||||
return (
|
||||
<Spoiler reason={reason}>{domToReact(span.children as DOMNode[], { replace: params.replace })}</Spoiler>
|
||||
);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
+18
-36
@@ -8,7 +8,6 @@ Please see LICENSE files in the repository root for full details.
|
||||
import React, { type JSX } from "react";
|
||||
import { type DOMNode, Element, type HTMLReactParserOptions, Text } from "html-react-parser";
|
||||
import { type MatrixEvent, type Room } from "matrix-js-sdk/src/matrix";
|
||||
import { type Opts } from "linkifyjs";
|
||||
|
||||
/**
|
||||
* The type of a parent node of an element, normally exported by domhandler but that is not a direct dependency of ours
|
||||
@@ -65,29 +64,9 @@ export function applyReplacerOnString(
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a Replacer function to a render function for linkify-react
|
||||
* So that we can use the same replacer functions for both
|
||||
* @param replacer The replacer function to convert
|
||||
*/
|
||||
export function replacerToRenderFunction(replacer: Replacer): Opts["render"] {
|
||||
if (!replacer) return;
|
||||
return ({ tagName, attributes, content }) => {
|
||||
const domNode = new Element(tagName, attributes, [new Text(content)], "tag" as Element["type"]);
|
||||
const result = replacer(domNode, 0);
|
||||
if (result) return result;
|
||||
|
||||
// This is cribbed from the default render function in linkify-react
|
||||
if (attributes.class) {
|
||||
attributes.className = attributes.class;
|
||||
delete attributes.class;
|
||||
}
|
||||
return React.createElement(tagName, attributes, content);
|
||||
};
|
||||
}
|
||||
|
||||
interface Parameters {
|
||||
isHtml: boolean;
|
||||
replace: Replacer;
|
||||
// Required for keywordPillRenderer
|
||||
keywordRegexpPattern?: RegExp;
|
||||
// Required for mentionPillRenderer
|
||||
@@ -114,7 +93,7 @@ export type RendererMap = Partial<
|
||||
}
|
||||
>;
|
||||
|
||||
type PreparedRenderer = (parameters: Parameters) => Replacer;
|
||||
type PreparedRenderer = (parameters: Omit<Parameters, "replace">) => Replacer;
|
||||
|
||||
/**
|
||||
* Combines multiple renderers into a single Replacer function
|
||||
@@ -122,19 +101,22 @@ type PreparedRenderer = (parameters: Parameters) => Replacer;
|
||||
*/
|
||||
export const combineRenderers =
|
||||
(...renderers: RendererMap[]): PreparedRenderer =>
|
||||
(parameters) =>
|
||||
(node, index) => {
|
||||
if (node.type === "text") {
|
||||
for (const replacer of renderers) {
|
||||
const result = replacer[Node.TEXT_NODE]?.(node, parameters, index);
|
||||
if (result) return result;
|
||||
(parameters) => {
|
||||
const replace: Replacer = (node, index) => {
|
||||
if (node.type === "text") {
|
||||
for (const replacer of renderers) {
|
||||
const result = replacer[Node.TEXT_NODE]?.(node, parametersWithReplace, index);
|
||||
if (result) return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (node instanceof Element) {
|
||||
const tagName = node.tagName.toLowerCase() as keyof HTMLElementTagNameMap;
|
||||
for (const replacer of renderers) {
|
||||
const result = replacer[tagName]?.(node, parameters, index);
|
||||
if (result) return result;
|
||||
if (node instanceof Element) {
|
||||
const tagName = node.tagName.toLowerCase() as keyof HTMLElementTagNameMap;
|
||||
for (const replacer of renderers) {
|
||||
const result = replacer[tagName]?.(node, parametersWithReplace, index);
|
||||
if (result) return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const parametersWithReplace: Parameters = { ...parameters, replace };
|
||||
return replace;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user