mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-05-25 23:20:19 +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;
|
inputElement: HTMLInputElement | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function escapeRegExp(search: string) {
|
||||||
|
return search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||||
|
}
|
||||||
|
|
||||||
function highlightAndShorten(str: string, search: string) {
|
function highlightAndShorten(str: string, search: string) {
|
||||||
const index = str.toLowerCase().indexOf(search.toLowerCase());
|
const index = str.toLowerCase().indexOf(search.toLowerCase());
|
||||||
const head = Math.max(0, index - 10);
|
const head = Math.max(0, index - 10);
|
||||||
const tail = Math.min(str.length, index + search.length + 40);
|
const tail = Math.min(str.length, index + search.length + 40);
|
||||||
// Remove code block syntax
|
// Remove code block syntax
|
||||||
let result = str.slice(head, tail);
|
let result = str.slice(head, tail);
|
||||||
|
const safeSearch = escapeRegExp(search);
|
||||||
// Use ** to highlight the search result
|
// Use ** to highlight the search result
|
||||||
result = result.replace(new RegExp(`(${search})`), "**$1**");
|
result = result.replace(new RegExp(`(${safeSearch})`), "**$1**");
|
||||||
|
|
||||||
if (head > 0) {
|
if (head > 0) {
|
||||||
result = "..." + result;
|
result = "..." + result;
|
||||||
|
Loading…
Reference in New Issue
Block a user