Skip to content

Commit

Permalink
fix: SearchModal crash on special character input (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
kangju2000 authored Sep 16, 2024
1 parent ef06e36 commit bc2284c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/components/Search/SearchModalContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as React from 'react'
import { useDocs } from '@/app/[...slug]/DocsContext'

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

// Get length of matched text in result
const relevanceOf = (result: SearchResult) =>
(result.title.toLowerCase().match(deferredQuery.toLowerCase())?.length ?? 0) /
(result.title.toLowerCase().match(escape(deferredQuery.toLowerCase()))?.length ?? 0) /
result.title.length

// Search
Expand Down
2 changes: 1 addition & 1 deletion src/utils/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export const escape = (text: string) => text.replace(/[|\\{}()[\]^$+*?.]/g, '\\$
*/
export const highlight = (text: string, target: string) =>
target.length > 0
? text.replace(new RegExp(target, 'gi'), (match: string) => `<mark>${match}</mark>`)
? text.replace(new RegExp(escape(target), 'gi'), (match: string) => `<mark>${match}</mark>`)
: text

0 comments on commit bc2284c

Please sign in to comment.