Skip to content

Commit

Permalink
refactor: markdown processor
Browse files Browse the repository at this point in the history
  • Loading branch information
rabyeoljji committed Dec 10, 2024
1 parent f793682 commit c3f7d20
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/features/search/components/search-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import DocumentTypeIcon from '@/features/document/components/document-type-icon'

interface Props {
createType: Document.ItemInList['documentType']
documentTitle: string
documentTitle: React.ReactNode
matchingSentence: React.ReactNode
resultType: 'document' | 'quiz'
relativeDirectory: string
Expand Down
23 changes: 5 additions & 18 deletions src/features/search/screen/search-in-document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import SearchList from '../components/search-list'
import SearchItem from '../components/search-item'
import RecentSearches from '../components/recent-searches'
import HeaderInDocument from '../components/header-in-document'
import { extractPlainText, highlightAndTrimText } from '../utils'
import { highlightAndTrimText, MarkdownProcessor } from '../utils'
import Text from '@/shared/components/ui/text'
import { useRouter, useSearchParams } from 'next/navigation'
import { useQuery } from '@tanstack/react-query'
Expand Down Expand Up @@ -123,7 +123,10 @@ const SearchInDocument = () => {
<SearchItem
key={idx}
createType={idx % 2 === 0 ? 'FILE' : 'TEXT'} // type 들어가야함
documentTitle={searchItem.documentName ?? ''}
documentTitle={highlightAndTrimText(
searchItem.documentName ?? '',
initialKeyword ?? ''
)}
matchingSentence={
searchItem.content ? (
<MarkdownProcessor
Expand Down Expand Up @@ -155,19 +158,3 @@ const SearchInDocument = () => {
}

export default SearchInDocument

/** 마크다운 텍스트를 받아
* 문법을 제거하고 키워드에 강조를 해서 반환하는 함수
*/
const MarkdownProcessor = ({
markdownText,
keyword,
}: {
markdownText: string
keyword: string
}) => {
const plainText = extractPlainText(markdownText)
const highlightedText = highlightAndTrimText(plainText, keyword)

return <div>{highlightedText}</div>
}
16 changes: 16 additions & 0 deletions src/features/search/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,19 @@ export function extractPlainText(markdownText: string): string {
div.innerHTML = html
return div.textContent || ''
}

/** 마크다운 텍스트를 받아
* 문법을 제거하고 키워드에 강조를 해서 반환하는 함수
*/
export const MarkdownProcessor = ({
markdownText,
keyword,
}: {
markdownText: string
keyword: string
}) => {
const plainText = extractPlainText(markdownText)
const highlightedText = highlightAndTrimText(plainText, keyword)

return <div>{highlightedText}</div>
}

0 comments on commit c3f7d20

Please sign in to comment.