From 46925a7597e99e6669f5921d5a426993296fe870 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Tue, 1 Mar 2022 17:14:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=85=E5=AD=98=E5=85=A8=E6=96=87=E7=B4=A2?= =?UTF-8?q?=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/search/[keyword].js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index 2909f317..4f7cd942 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -25,8 +25,9 @@ function appendText (sourceTextArray, targetObj, key) { const textArray = targetObj[key] const text = textArray ? textArray[0][0] : '' if (text && text !== 'Untitled') { - sourceTextArray.concat(text) + return sourceTextArray.concat(text) } + return sourceTextArray } export async function getStaticProps ({ params: { keyword } }) { @@ -43,28 +44,34 @@ export async function getStaticProps ({ params: { keyword } }) { for (const post of allPosts) { const cacheKey = 'page_block_' + post.id const page = await getDataFromCache(cacheKey) - console.log('读取缓存结果:', cacheKey, page) const tagContent = post.tags ? post.tags.join(' ') : '' const categoryContent = post.category ? post.category.join(' ') : '' - const indexContent = [post.title, post.summary, tagContent, categoryContent] - if (page != null) { + let indexContent = [post.title, post.summary, tagContent, categoryContent] + if (page !== null) { const contentIds = Object.keys(page.block) contentIds.forEach(id => { const properties = page?.block[id]?.value?.properties - appendText(indexContent, properties, 'title') - appendText(indexContent, properties, 'caption') + indexContent = appendText(indexContent, properties, 'title') + indexContent = appendText(indexContent, properties, 'caption') }) } + if (page !== null) { + console.log('读取缓存成功', page, indexContent) + } post.results = [] + let hit = false indexContent.forEach(c => { const index = c.toLowerCase().indexOf(keyword.toLowerCase()) if (index > -1) { + hit = true const referText = c?.replaceAll(keyword, `${keyword}`) post.results.push(`${referText}`) + } else { + post.results.push(`${c}`) } }) - if (post.results.length > 0) { + if (hit) { filterPosts.push(post) } }