From 5c07e3ced669db156b266865ef7956c6f3379a2d Mon Sep 17 00:00:00 2001 From: John Regan Date: Wed, 5 Jul 2023 14:23:39 -0400 Subject: [PATCH] highlighting: make case-insensitive, support unicode (#3137) * highlighting: make case-insensitive, support unicode * highlighting: also highlight simplified/normalized form * highlighting: use MDN-recommended escape pattern --- .../chat/ChatUserMessage/customMatcher.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/web/components/chat/ChatUserMessage/customMatcher.ts b/web/components/chat/ChatUserMessage/customMatcher.ts index d6136da1a..b08d01183 100644 --- a/web/components/chat/ChatUserMessage/customMatcher.ts +++ b/web/components/chat/ChatUserMessage/customMatcher.ts @@ -19,7 +19,18 @@ export class ChatMessageHighlightMatcher extends Matcher { return null; } - const result = str.match(highlightString); + 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')); if (!result) { return null;