mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-05-25 07:00:23 +09:00
fix: search special character error
This commit is contained in:
parent
f43c5c019f
commit
5a23a4d217
@ -37,15 +37,19 @@ export interface SearchInputRef {
|
||||
inputElement: HTMLInputElement | null;
|
||||
}
|
||||
|
||||
function escapeRegExp(search: string) {
|
||||
return search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
function highlightAndShorten(str: string, search: string) {
|
||||
const index = str.toLowerCase().indexOf(search.toLowerCase());
|
||||
const head = Math.max(0, index - 10);
|
||||
const tail = Math.min(str.length, index + search.length + 40);
|
||||
// Remove code block syntax
|
||||
let result = str.slice(head, tail);
|
||||
|
||||
const safeSearch = escapeRegExp(search);
|
||||
// Use ** to highlight the search result
|
||||
result = result.replace(new RegExp(`(${search})`), "**$1**");
|
||||
result = result.replace(new RegExp(`(${safeSearch})`), "**$1**");
|
||||
|
||||
if (head > 0) {
|
||||
result = "..." + result;
|
||||
|
Loading…
Reference in New Issue
Block a user