2023-02-05 04:21:06 +03:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2023-07-05 21:23:39 +03:00
|
|
|
const escapedString = highlightString
|
|
|
|
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
|
|
.replace(/\s/g, '\\s');
|
|
|
|
|
|
|
|
const normalizedString = escapedString.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
|
|
|
|
|
|
|
|
let highlightRegex = escapedString;
|
|
|
|
if (escapedString !== normalizedString) {
|
|
|
|
highlightRegex = `(?:${escapedString})|(?:${normalizedString})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = str.match(new RegExp(highlightRegex, 'ui'));
|
2023-02-05 04:21:06 +03:00
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|