From fa67a6c4ed0015d66a08abbd92ff1ce2496429a0 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Tue, 1 Mar 2022 16:45:16 +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 | 36 ++++++++++++-------------- themes/NEXT/Layout404.js | 2 +- themes/NEXT/components/BlogPostCard.js | 4 +-- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index 00c6d891..f6b94f18 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -13,21 +13,20 @@ export async function getStaticPaths () { /** * 将对象的指定字段拼接到字符串 - * @param sourceText + * @param sourceTextArray * @param targetObj * @param key * @returns {*} */ -function concatPropsText (sourceText, targetObj, key) { +function appendText (sourceTextArray, targetObj, key) { if (!targetObj) { - return sourceText + return sourceTextArray } const textArray = targetObj[key] const text = textArray ? textArray[0][0] : '' if (text && text !== 'Untitled') { - return sourceText.concat(text) + sourceTextArray.concat(text) } - return sourceText } export async function getStaticProps ({ params: { keyword } }) { @@ -44,32 +43,29 @@ export async function getStaticProps ({ params: { keyword } }) { allPosts.forEach(post => { const cacheKey = 'page_block_' + post.id const page = cache.get(cacheKey) + console.log('读取缓存结果:', cacheKey, page) const tagContent = post.tags ? post.tags.join(' ') : '' const categoryContent = post.category ? post.category.join(' ') : '' - let indexContent = post.title + ' ' + post.summary + ' ' + tagContent + ' ' + categoryContent + const 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 - indexContent = concatPropsText(indexContent, properties, 'title') - indexContent = concatPropsText(indexContent, properties, 'caption') + appendText(indexContent, properties, 'title') + appendText(indexContent, properties, 'caption') }) } post.results = [] - const index = indexContent.indexOf(keyword) - if (index > -1) { - filterPosts.push(post) - let start = index - 20 - let end = index + 20 - if (start < 0) start = 0 - if (end > indexContent.length) end = indexContent.length - const referText = indexContent.substr(start, end).replaceAll(keyword, `${keyword}`) - post.results.push(`${referText}`) - } + indexContent.forEach(c => { + const index = c.indexOf(keyword) + if (index > -1) { + filterPosts.push(post) + const referText = c?.replaceAll(keyword, `${keyword}`) + post.results.push(`${referText}`) + } + }) }) - console.log(filterPosts) - return { props: { posts: filterPosts, diff --git a/themes/NEXT/Layout404.js b/themes/NEXT/Layout404.js index 9c51b293..2fcfc7aa 100644 --- a/themes/NEXT/Layout404.js +++ b/themes/NEXT/Layout404.js @@ -23,7 +23,7 @@ export const Layout404 = (props) => {
-

404

+

404

页面无法加载,即将返回首页

diff --git a/themes/NEXT/components/BlogPostCard.js b/themes/NEXT/components/BlogPostCard.js index e1461730..ee31f80e 100644 --- a/themes/NEXT/components/BlogPostCard.js +++ b/themes/NEXT/components/BlogPostCard.js @@ -42,10 +42,10 @@ const BlogPostCard = ({ post, showSummary }) => {
{(!showPreview || showSummary) &&

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