Skip to content

Commit

Permalink
fix: handle special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
ansh-saini committed Oct 27, 2024
1 parent bbc6098 commit b215dce
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions components/ProjectCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,26 @@ interface ProjectCardBodyProps {
searchQuery?: string;
}

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

const highlightText = (text: string, query: string) => {
if (!query) return text;
const parts = text.split(new RegExp(`(${query})`, 'gi'));
return parts.map((part, index) =>
part.toLowerCase() === query.toLowerCase() ? (
<mark key={index}>{part}</mark>
) : (
part
),
);

try {
const escapedQuery = escapeSpecialCharacters(query);
const parts = text.split(new RegExp(`(${escapedQuery})`, 'gi'));
return parts.map((part, index) =>
part.toLowerCase() === query.toLowerCase() ? (
<mark key={index}>{part}</mark>
) : (
part
),
);
} catch (e) {
return text;
}
};

function ProjectCardBody({
Expand Down

0 comments on commit b215dce

Please sign in to comment.