Skip to content

Commit bc2284c

Browse files
authored
fix: SearchModal crash on special character input (#335)
1 parent ef06e36 commit bc2284c

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

src/components/Search/SearchModalContainer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as React from 'react'
44
import { useDocs } from '@/app/[...slug]/DocsContext'
55

66
import cn from '@/lib/cn'
7+
import { escape } from '@/utils/text'
78
import { Command } from 'cmdk'
89
import { useRouter } from 'next/navigation'
910
import { ComponentProps } from 'react'
@@ -27,7 +28,7 @@ export const SearchModalContainer = ({
2728

2829
// Get length of matched text in result
2930
const relevanceOf = (result: SearchResult) =>
30-
(result.title.toLowerCase().match(deferredQuery.toLowerCase())?.length ?? 0) /
31+
(result.title.toLowerCase().match(escape(deferredQuery.toLowerCase()))?.length ?? 0) /
3132
result.title.length
3233

3334
// Search

src/utils/text.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ export const escape = (text: string) => text.replace(/[|\\{}()[\]^$+*?.]/g, '\\$
88
*/
99
export const highlight = (text: string, target: string) =>
1010
target.length > 0
11-
? text.replace(new RegExp(target, 'gi'), (match: string) => `<mark>${match}</mark>`)
11+
? text.replace(new RegExp(escape(target), 'gi'), (match: string) => `<mark>${match}</mark>`)
1212
: text

0 commit comments

Comments
 (0)