From 2fbe4b736f23a7bc6ec8321cf0e15fff52e46c05 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 2 Mar 2022 10:32:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E6=96=87=E7=B4=A2=E5=BC=95=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/notion/getPostBlocks.js | 6 +++++- pages/search/[keyword].js | 22 ++++++++++++++++++---- themes/NEXT/components/SideAreaLeft.js | 4 ++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/notion/getPostBlocks.js b/lib/notion/getPostBlocks.js index b2ef8f19..ee4fae68 100644 --- a/lib/notion/getPostBlocks.js +++ b/lib/notion/getPostBlocks.js @@ -2,7 +2,7 @@ import BLOG from '@/blog.config' import { NotionAPI } from 'notion-client' import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager' -export async function getPostBlocks (id, from, slice) { +export async function getPostBlocks (id, from, slice, retryCount = 3) { const cacheKey = 'page_block_' + id let pageBlock = await getDataFromCache(cacheKey) if (pageBlock) { @@ -17,6 +17,10 @@ export async function getPostBlocks (id, from, slice) { console.log('[请求成功]', `from:${from}`, `id:${id}`) } catch (error) { console.error('[请求失败]', `from:${from}`, `id:${id}`, `error:${error}`) + if (retryCount && retryCount > 0) { // 重试 + console.error('[重试请求]', `from:${from}`, `id:${id}`, `剩余次数:${retryCount}`) + return getPostBlocks(id, from, slice, retryCount - 1) + } return null } diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index d1dff9c2..8463fc91 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -23,13 +23,30 @@ function appendText (sourceTextArray, targetObj, key) { return sourceTextArray } const textArray = targetObj[key] - const text = textArray ? textArray[0][0] : '' + const text = textArray ? getTextContent(textArray) : '' if (text && text !== 'Untitled') { return sourceTextArray.concat(text) } return sourceTextArray } +/** + * 递归获取层层嵌套的数组 + * @param {*} textArray + * @returns + */ +function getTextContent (textArray) { + if (typeof textArray === 'object') { + let result = '' + textArray.forEach(textObj => { + result = result + getTextContent(textObj) + }) + return result + } else if (typeof textArray === 'string') { + return textArray + } +} + export async function getStaticProps ({ params: { keyword } }) { const { allPosts, @@ -55,9 +72,6 @@ export async function getStaticProps ({ params: { keyword } }) { indexContent = appendText(indexContent, properties, 'caption') }) } - if (page !== null) { - console.log('读取缓存成功', page, indexContent) - } post.results = [] let hit = false const re = new RegExp(`${keyword}`, 'g') diff --git a/themes/NEXT/components/SideAreaLeft.js b/themes/NEXT/components/SideAreaLeft.js index e59662be..9eb9a372 100644 --- a/themes/NEXT/components/SideAreaLeft.js +++ b/themes/NEXT/components/SideAreaLeft.js @@ -19,7 +19,7 @@ import CONFIG_NEXT from '../config_next' * @constructor */ const SideAreaLeft = (props) => { - const { currentTag, post, postCount, currentSearch } = props + const { post, postCount } = props const { locale } = useGlobal() const showToc = post && post.toc && post.toc.length > 1 return