Refactor how message content highlighting works + change to safe HTML rendering component. Closes #2669

This commit is contained in:
Gabe Kangas
2023-02-04 17:21:06 -08:00
parent 388e4d3d78
commit e6d3da4f9c
7 changed files with 148 additions and 67 deletions

View File

@@ -0,0 +1,44 @@
/* eslint-disable class-methods-use-this */
import { ChildrenNode, Matcher, MatchResponse, Node } from 'interweave';
import React from 'react';
export interface CustomProps {
children: React.ReactNode;
key: string;
}
interface options {
highlightString: string;
}
export class ChatMessageHighlightMatcher extends Matcher {
match(str: string): MatchResponse<{}> | null {
const { highlightString } = this.options as options;
if (!highlightString) {
return null;
}
const result = str.match(highlightString);
if (!result) {
return null;
}
return {
index: result.index!,
length: result[0].length,
match: result[0],
valid: true,
};
}
replaceWith(children: ChildrenNode, props: CustomProps): Node {
const { key } = props;
return React.createElement('mark', { key }, children);
}
asTag(): string {
return 'mark';
}
}