diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index f6b94f18..2909f317 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -2,7 +2,7 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData' import { LayoutSearch } from '@/themes' import BLOG from '@/blog.config' import { useGlobal } from '@/lib/global' -import cache from 'memory-cache' +import { getDataFromCache } from '@/lib/cache/cache_manager' export async function getStaticPaths () { return { @@ -40,9 +40,9 @@ export async function getStaticProps ({ params: { keyword } }) { } = await getGlobalNotionData({ from: 'search-props', pageType: ['Post'] }) const filterPosts = [] - allPosts.forEach(post => { + for (const post of allPosts) { const cacheKey = 'page_block_' + post.id - const page = cache.get(cacheKey) + const page = await getDataFromCache(cacheKey) console.log('读取缓存结果:', cacheKey, page) const tagContent = post.tags ? post.tags.join(' ') : '' const categoryContent = post.category ? post.category.join(' ') : '' @@ -57,14 +57,17 @@ export async function getStaticProps ({ params: { keyword } }) { } post.results = [] indexContent.forEach(c => { - const index = c.indexOf(keyword) + const index = c.toLowerCase().indexOf(keyword.toLowerCase()) if (index > -1) { - filterPosts.push(post) const referText = c?.replaceAll(keyword, `${keyword}`) post.results.push(`${referText}`) } }) - }) + + if (post.results.length > 0) { + filterPosts.push(post) + } + } return { props: { diff --git a/themes/NEXT/components/BlogPostCard.js b/themes/NEXT/components/BlogPostCard.js index ee31f80e..2e104601 100644 --- a/themes/NEXT/components/BlogPostCard.js +++ b/themes/NEXT/components/BlogPostCard.js @@ -41,16 +41,16 @@ const BlogPostCard = ({ post, showSummary }) => { - {(!showPreview || showSummary) &&

- {post.summary && !post.results} - {/* 搜索结果 */} - {post.results && - {post.results.map(r => ...)} - - } - + {(!showPreview || showSummary) && !post.results &&

+ {post.summary}

} + {/* 搜索结果 */} + {post.results &&

+ {post.results.map(r => ...)} +

+ } + {showPreview && post?.blockMap &&