清理冗余搜索

This commit is contained in:
tangly1024
2022-02-28 17:30:17 +08:00
parent f86ff24807
commit 6c91e68ff9
12 changed files with 58 additions and 135 deletions

View File

@@ -1,5 +1,8 @@
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { LayoutSearch } from '@/themes'
import BLOG from '@/blog.config'
import { useGlobal } from '@/lib/global'
import { useRouter } from 'next/router'
export async function getStaticProps () {
const {
@@ -24,7 +27,36 @@ export async function getStaticProps () {
}
const Search = (props) => {
return <LayoutSearch {...props} />
const { posts } = props
let filteredPosts
const searchKey = getSearchKey()
// 静态过滤
if (searchKey) {
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(searchKey.toLowerCase())
})
} else {
filteredPosts = posts
}
const { locale } = useGlobal()
const meta = {
title: `${searchKey || ''} | ${locale.NAV.SEARCH} | ${BLOG.TITLE} `,
description: BLOG.DESCRIPTION,
type: 'website'
}
return <LayoutSearch {...props} posts={filteredPosts} meta={meta} currentSearch={searchKey} />
}
function getSearchKey () {
const router = useRouter()
if (router.query && router.query.s) {
return router.query.s
}
return null
}
export default Search