|
| 1 | +import BLOG from '@/blog.config' |
| 2 | +import { BlogListPage } from './components/BlogListPage' |
| 3 | +import { BlogListScroll } from './components/BlogListScroll' |
| 4 | +import { useRouter } from 'next/router' |
| 5 | +import { useEffect } from 'react' |
| 6 | +import SearchInput from './components/SearchInput' |
| 7 | +import Mark from 'mark.js' |
| 8 | +import LayoutBase from './LayoutBase' |
| 9 | +import { isBrowser } from '@/lib/utils' |
| 10 | + |
| 11 | +export const LayoutSearch = props => { |
| 12 | + const { keyword } = props |
| 13 | + const router = useRouter() |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + setTimeout(() => { |
| 17 | + const container = isBrowser() && document.getElementById('container') |
| 18 | + if (container && container.innerHTML) { |
| 19 | + const re = new RegExp(keyword, 'gim') |
| 20 | + const instance = new Mark(container) |
| 21 | + instance.markRegExp(re, { |
| 22 | + element: 'span', |
| 23 | + className: 'text-red-500 border-b border-dashed' |
| 24 | + }) |
| 25 | + } |
| 26 | + }, 100) |
| 27 | + }, [router.events]) |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + setTimeout(() => { |
| 31 | + if (keyword) { |
| 32 | + const targets = document.getElementsByClassName('replace') |
| 33 | + for (const container of targets) { |
| 34 | + if (container && container.innerHTML) { |
| 35 | + const re = new RegExp(`${keyword}`, 'gim') |
| 36 | + container.innerHTML = container.innerHTML.replace( |
| 37 | + re, |
| 38 | + `<span class='text-red-500 border-b border-dashed'>${keyword}</span>` |
| 39 | + ) |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + }, 100) |
| 44 | + }, []) |
| 45 | + |
| 46 | + return <LayoutBase {...props}> |
| 47 | + <div className='pb-12'> |
| 48 | + <SearchInput {...props} /> |
| 49 | + </div> |
| 50 | + |
| 51 | + {BLOG.POST_LIST_STYLE === 'page' ? <BlogListPage {...props} /> : <BlogListScroll {...props} />} |
| 52 | + |
| 53 | + </LayoutBase> |
| 54 | +} |
0 commit comments