Skip to content

Commit

Permalink
fixed the crash issue on search bar in thread
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammadKaleem171 committed Oct 8, 2024
1 parent cf84059 commit 3ff312a
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import { findAll } from 'highlight-words-core';
import React from 'react';
import smartTruncate from 'smart-truncate';

const escapeRegExp = (string: string) => {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // Escaping special characters
};

export const renderTruncatedHighlights = (
searchTerm: string,
docText: string,
) => {
// extract highlighted text
const words = searchTerm.split(/\s+/); // Split on whitespace
// Escape each word in the search term
const escapedWords = words.map((word) => escapeRegExp(word));
const chunks = findAll({
searchWords: [searchTerm.trim()],
searchWords: escapedWords,
textToHighlight: docText,
});

Expand Down

0 comments on commit 3ff312a

Please sign in to comment.