Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/NotePage/NotePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useGeneralStatus } from '../../lib/generalStatus'
import { useDialog, DialogIconTypes } from '../../lib/dialog'
import FileDropZone from '../atoms/FileDropZone'
import { convertCSONFileToNote } from '../../lib/legacy-import'
import { escapeRegExp } from '../../lib/regex'

export const StyledNoteDetailNoNote = styled.div`
text-align: center;
Expand Down Expand Up @@ -81,7 +82,7 @@ export default () => {

const filteredNotes = useMemo(() => {
if (search.trim() === '') return notes
const regex = new RegExp(search, 'i')
const regex = new RegExp(escapeRegExp(search), 'i')
return notes.filter(
note =>
note.tags.join().match(regex) ||
Expand Down
3 changes: 2 additions & 1 deletion src/components/atoms/HighlightText.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useMemo } from 'react'
import styled from '../../lib/styled'
import { escapeRegExp } from '../../lib/regex'

const HighlightContainer = styled.span`
.highlighted {
Expand All @@ -16,7 +17,7 @@ function HighlightText(props: HighlightTextProps) {
const { text, search } = props
return useMemo(() => {
if (search.trim() === '') return <>{text}</>
const searchRegex = new RegExp(`(${search})`, 'gi')
const searchRegex = new RegExp(`(${escapeRegExp(search)})`, 'gi')
const parts = text.split(searchRegex)

return (
Expand Down
3 changes: 3 additions & 0 deletions src/lib/regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function escapeRegExp(str: string) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}