Implement search functionality - Add searchItems server action with list-scoped and global search - Add highlightText utility function for search result highlighting - Search across title, description, notes, and tags fields - Requirements: 8.1, 8.2, 8.5
This commit is contained in:
23
lib/utils.ts
Normal file
23
lib/utils.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Highlights matching text in a string by wrapping matches with a marker
|
||||
* @param text - The text to search in
|
||||
* @param query - The search query to highlight
|
||||
* @returns The text with highlighted matches wrapped in <mark> tags
|
||||
*/
|
||||
export function highlightText(text: string, query: string): string {
|
||||
if (!query.trim() || !text) {
|
||||
return text
|
||||
}
|
||||
|
||||
const regex = new RegExp(`(${escapeRegExp(query)})`, 'gi')
|
||||
return text.replace(regex, '<mark>$1</mark>')
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes special regex characters in a string
|
||||
* @param string - The string to escape
|
||||
* @returns The escaped string safe for use in RegExp
|
||||
*/
|
||||
function escapeRegExp(string: string): string {
|
||||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||
}
|
||||
Reference in New Issue
Block a user