import { useRouter } from 'next/router' import { useEffect, useRef } from 'react' import BLOG from '@/blog.config' import BlogPostListScroll from './components/BlogPostListScroll' import BlogPostListPage from './components/BlogPostListPage' import LayoutBase from './LayoutBase' import SearchInput from './components/SearchInput' import { useGlobal } from '@/lib/global' import Mark from 'mark.js' import TagItemMini from './components/TagItemMini' import Card from './components/Card' import Link from 'next/link' export const LayoutSearch = props => { const { keyword, tagOptions, categoryOptions } = props const { locale } = useGlobal() const router = useRouter() const currentSearch = keyword || router?.query?.s const cRef = useRef(null) useEffect(() => { setTimeout(() => { // 自动聚焦到搜索框 cRef?.current?.focus() if (currentSearch) { const targets = document.getElementsByClassName('replace') for (const container of targets) { if (container && container.innerHTML) { const re = new RegExp(currentSearch, 'gim') const instance = new Mark(container) instance.markRegExp(re, { element: 'span', className: 'text-red-500 border-b border-dashed' }) } } } }, 100) }) return ( {!currentSearch && <>
{/* 分类 */}
{locale.COMMON.CATEGORY}:
{categoryOptions?.map(category => { return (
{category.name}({category.count})
) })}
{/* 标签 */}
{locale.COMMON.TAGS}:
{tagOptions?.map(tag => { return (
) })}
} {currentSearch && <>
{BLOG.POST_LIST_STYLE === 'page' ? : }
}
) }