|
import { getGlobalData } from '@/lib/notion/getNotionData' |
|
import { useRouter } from 'next/router' |
|
import BLOG from '@/blog.config' |
|
import { getLayoutByTheme } from '@/themes/theme' |
|
import { siteConfig } from '@/lib/config' |
|
|
|
|
|
|
|
|
|
|
|
|
|
const Search = props => { |
|
const { posts } = props |
|
|
|
|
|
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() }) |
|
|
|
const router = useRouter() |
|
const keyword = router?.query?.s |
|
|
|
let filteredPosts |
|
|
|
if (keyword) { |
|
filteredPosts = posts.filter(post => { |
|
const tagContent = post?.tags ? post?.tags.join(' ') : '' |
|
const categoryContent = post.category ? post.category.join(' ') : '' |
|
const searchContent = |
|
post.title + post.summary + tagContent + categoryContent |
|
return searchContent.toLowerCase().includes(keyword.toLowerCase()) |
|
}) |
|
} else { |
|
filteredPosts = [] |
|
} |
|
|
|
props = { ...props, posts: filteredPosts } |
|
|
|
return <Layout {...props} /> |
|
} |
|
|
|
|
|
|
|
|
|
export async function getStaticProps() { |
|
const props = await getGlobalData({ |
|
from: 'search-props', |
|
pageType: ['Post'] |
|
}) |
|
const { allPages } = props |
|
props.posts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published') |
|
return { |
|
props, |
|
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) |
|
} |
|
} |
|
|
|
export default Search |
|
|