diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index 3e74b996..c3bfaeff 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -2,14 +2,7 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData' import { LayoutSearch } from '@/themes' import BLOG from '@/blog.config' import { useGlobal } from '@/lib/global' -import { getDataFromCache } from '@/lib/cache/cache_manager' - -export async function getStaticPaths () { - return { - paths: [], - fallback: true - } -} +import { getPostBlocks } from '@/lib/notion/getPostBlocks' /** * 将对象的指定字段拼接到字符串 @@ -38,16 +31,16 @@ function appendText (sourceTextArray, targetObj, key) { function getTextContent (textArray) { if (typeof textArray === 'object') { let result = '' - textArray.forEach(textObj => { + for (const textObj of textArray) { result = result + getTextContent(textObj) - }) + } return result } else if (typeof textArray === 'string') { return textArray } } -export async function getStaticProps ({ params: { keyword } }) { +export async function getServerSideProps ({ params: { keyword } }) { const { allPosts, categories, @@ -59,14 +52,13 @@ export async function getStaticProps ({ params: { keyword } }) { const filterPosts = [] for (const post of allPosts) { - const cacheKey = 'page_block_' + post.id - const page = await getDataFromCache(cacheKey) + // const cacheKey = 'page_block_' + post.id + const page = await getPostBlocks(post.id, 'search') const tagContent = post.tags ? post.tags.join(' ') : '' const categoryContent = post.category ? post.category.join(' ') : '' const articleInfo = post.title + post.summary + tagContent + categoryContent let hit = articleInfo.indexOf(keyword) > -1 let indexContent = [post.summary] - console.log('搜索是否命中缓存', page !== null) if (page !== null) { const contentIds = Object.keys(page.block) contentIds.forEach(id => { @@ -75,23 +67,25 @@ export async function getStaticProps ({ params: { keyword } }) { indexContent = appendText(indexContent, properties, 'caption') }) } + console.log('搜索是否命中缓存', page !== null, indexContent) + post.results = [] let hitCount = 0 - const re = new RegExp(`${keyword}`, 'g') - indexContent.forEach((c, i) => { + const re = new RegExp(`${keyword}`, 'gim') + for (const i in indexContent) { + const c = indexContent[i] const index = c.toLowerCase().indexOf(keyword.toLowerCase()) + const referText = c?.replace(re, `${keyword}`) if (index > -1) { hit = true - const referText = c?.replace(re, `${keyword}`) - post.results.push(`${referText}`) hitCount += 1 + post.results.push(`${referText}`) } else { if ((post.results.length - 1) / hitCount < 3 || i === 0) { - post.results.push(`${c}`) + post.results.push(`${referText}`) } } - }) - + } if (hit) { filterPosts.push(post) } @@ -106,8 +100,7 @@ export async function getStaticProps ({ params: { keyword } }) { latestPosts, customNav, keyword - }, - revalidate: 1 + } } } diff --git a/themes/NEXT/components/SearchInput.js b/themes/NEXT/components/SearchInput.js index 4ee8165a..c3565c43 100644 --- a/themes/NEXT/components/SearchInput.js +++ b/themes/NEXT/components/SearchInput.js @@ -2,6 +2,8 @@ import { useRouter } from 'next/router' import { useGlobal } from '@/lib/global' import { useImperativeHandle, useRef, useState } from 'react' +let lock = false + const SearchInput = ({ currentTag, currentSearch, cRef }) => { const { locale } = useGlobal() // const [searchKey, setSearchKey] = useState(currentSearch || '') @@ -39,7 +41,6 @@ const SearchInput = ({ currentTag, currentSearch, cRef }) => { searchInputRef.current.value = '' setShowClean(false) } - let lock = false function lockSearchInput () { lock = true }