fix: KnowledgeSearchPopup keyword highlighting issue

This commit is contained in:
Yrom Wang 2025-02-07 12:16:18 +05:30 committed by 亢奋猫
parent 496b4684ea
commit acda36ae3f

View File

@ -69,7 +69,11 @@ const PopupContainer: React.FC<Props> = ({ base, resolve }) => {
const highlightText = (text: string) => { const highlightText = (text: string) => {
if (!searchKeyword) return text if (!searchKeyword) return text
const parts = text.split(new RegExp(`(${searchKeyword})`, 'gi'))
// Escape special characters in the search keyword
const escapedKeyword = searchKeyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
const parts = text.split(new RegExp(`(${escapedKeyword})`, 'gi'))
return parts.map((part, i) => return parts.map((part, i) =>
part.toLowerCase() === searchKeyword.toLowerCase() ? <mark key={i}>{part}</mark> : part part.toLowerCase() === searchKeyword.toLowerCase() ? <mark key={i}>{part}</mark> : part
) )