From f053f42c21076a679d0e4488f89609be8abcc12b Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Tue, 1 Mar 2022 16:23:09 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E5=86=85=E5=AD=98=E5=85=A8=E6=96=87?= =?UTF-8?q?=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index.js | 1 - pages/page/[page].js | 1 - pages/search/[keyword].js | 98 ++++++++++++++++++++ pages/{search.js => search/index.js} | 0 themes/NEXT/components/BlogPostCard.js | 6 ++ themes/NEXT/components/BlogPostListScroll.js | 2 +- themes/NEXT/components/SearchInput.js | 2 +- themes/index.js | 4 +- 8 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 pages/search/[keyword].js rename pages/{search.js => search/index.js} (100%) diff --git a/pages/index.js b/pages/index.js index be549212..599b3cf9 100644 --- a/pages/index.js +++ b/pages/index.js @@ -27,7 +27,6 @@ export async function getStaticProps () { BLOG.POSTS_PER_PAGE * page ) if (THEME_CONFIG.POST_LIST_PREVIEW || BLOG.POST_LIST_PREVIEW) { - console.log('加载预览') for (const i in postsToShow) { const post = postsToShow[i] const blockMap = await getPostBlocks(post.id, 'slug', BLOG.POST_PREVIEW_LINES) diff --git a/pages/page/[page].js b/pages/page/[page].js index f0f67312..211a2f92 100644 --- a/pages/page/[page].js +++ b/pages/page/[page].js @@ -45,7 +45,6 @@ export async function getStaticProps ({ params: { page } }) { ) // 加载预览 if (THEME_CONFIG.POST_LIST_PREVIEW || BLOG.POST_LIST_PREVIEW) { - console.log('加载预览') for (const i in postsToShow) { const post = postsToShow[i] const blockMap = await getPostBlocks(post.id, 'slug', BLOG.POST_PREVIEW_LINES) diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js new file mode 100644 index 00000000..00c6d891 --- /dev/null +++ b/pages/search/[keyword].js @@ -0,0 +1,98 @@ +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' + +export async function getStaticPaths () { + return { + paths: [], + fallback: true + } +} + +/** + * 将对象的指定字段拼接到字符串 + * @param sourceText + * @param targetObj + * @param key + * @returns {*} + */ +function concatPropsText (sourceText, targetObj, key) { + if (!targetObj) { + return sourceText + } + const textArray = targetObj[key] + const text = textArray ? textArray[0][0] : '' + if (text && text !== 'Untitled') { + return sourceText.concat(text) + } + return sourceText +} + +export async function getStaticProps ({ params: { keyword } }) { + const { + allPosts, + categories, + tags, + postCount, + latestPosts, + customNav + } = await getGlobalNotionData({ from: 'search-props', pageType: ['Post'] }) + + const filterPosts = [] + allPosts.forEach(post => { + const cacheKey = 'page_block_' + post.id + const page = cache.get(cacheKey) + const tagContent = post.tags ? post.tags.join(' ') : '' + const categoryContent = post.category ? post.category.join(' ') : '' + 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 + indexContent = concatPropsText(indexContent, properties, 'title') + indexContent = concatPropsText(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}`) + } + }) + + console.log(filterPosts) + + return { + props: { + posts: filterPosts, + tags, + categories, + postCount, + latestPosts, + customNav, + keyword + }, + revalidate: 1 + } +} + +const Index = (props) => { + const { keyword } = props + const { locale } = useGlobal() + const meta = { + title: `${keyword || ''} | ${locale.NAV.SEARCH} | ${BLOG.TITLE} `, + description: BLOG.DESCRIPTION, + type: 'website' + } + return +} + +export default Index diff --git a/pages/search.js b/pages/search/index.js similarity index 100% rename from pages/search.js rename to pages/search/index.js diff --git a/themes/NEXT/components/BlogPostCard.js b/themes/NEXT/components/BlogPostCard.js index d2f2c9be..e1461730 100644 --- a/themes/NEXT/components/BlogPostCard.js +++ b/themes/NEXT/components/BlogPostCard.js @@ -43,6 +43,12 @@ const BlogPostCard = ({ post, showSummary }) => { {(!showPreview || showSummary) &&

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

} {showPreview && post?.blockMap &&
diff --git a/themes/NEXT/components/BlogPostListScroll.js b/themes/NEXT/components/BlogPostListScroll.js index 8e520392..a220c8ad 100644 --- a/themes/NEXT/components/BlogPostListScroll.js +++ b/themes/NEXT/components/BlogPostListScroll.js @@ -65,7 +65,7 @@ const BlogPostListScroll = ({ posts = [], currentSearch, showSummary = CONFIG_NE
{ handleGetMore() }} - className='w-full my-4 py-4 text-center cursor-pointer glassmorphism shadow-xl rounded-xl dark:text-gray-200' + className='w-full my-4 py-4 text-center cursor-pointer glassmorphism shadow hover:shadow-xl duration-200 dark:text-gray-200' > {hasMore ? locale.COMMON.MORE : `${locale.COMMON.NO_MORE} 😰`}
diff --git a/themes/NEXT/components/SearchInput.js b/themes/NEXT/components/SearchInput.js index 48399f85..f676a4fd 100644 --- a/themes/NEXT/components/SearchInput.js +++ b/themes/NEXT/components/SearchInput.js @@ -18,7 +18,7 @@ const SearchInput = ({ currentTag, currentSearch, cRef }) => { const handleSearch = (key) => { if (key && key !== '') { setLoadingState(true) - router.push({ pathname: '/search', query: { s: key } }).then(r => { + router.push({ pathname: '/search/' + key }).then(r => { setLoadingState(false) }) } else { diff --git a/themes/index.js b/themes/index.js index d17405e5..e9b63872 100644 --- a/themes/index.js +++ b/themes/index.js @@ -3,7 +3,7 @@ */ // export * from './Empty' // 空主题 -// export * from './NEXT' +export * from './NEXT' // export * from './Fukasawa' // export * from './Hexo' -export * from './Medium' +// export * from './Medium' From fa67a6c4ed0015d66a08abbd92ff1ce2496429a0 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Tue, 1 Mar 2022 16:45:16 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E5=86=85=E5=AD=98=E5=85=A8=E6=96=87?= =?UTF-8?q?=E7=B4=A2=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 => ...)} } From edc008e6f4ec7629609c987ace04629f51ef4373 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Tue, 1 Mar 2022 17:02:51 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E5=86=85=E5=AD=98=E5=85=A8=E6=96=87?= =?UTF-8?q?=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/search/[keyword].js | 15 +++++++++------ themes/NEXT/components/BlogPostCard.js | 16 ++++++++-------- 2 files changed, 17 insertions(+), 14 deletions(-) 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 &&
Date: Tue, 1 Mar 2022 17:14:29 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E5=86=85=E5=AD=98=E5=85=A8=E6=96=87?= =?UTF-8?q?=E7=B4=A2=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) } } From 5bb730d4c2c986d9692d9524d5772b15be12e613 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Tue, 1 Mar 2022 17:35:29 +0800 Subject: [PATCH 05/11] =?UTF-8?q?replaceAll=E6=96=B9=E6=B3=95=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/search/[keyword].js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index 4f7cd942..d1dff9c2 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -60,11 +60,12 @@ export async function getStaticProps ({ params: { keyword } }) { } post.results = [] let hit = false + const re = new RegExp(`${keyword}`, 'g') indexContent.forEach(c => { const index = c.toLowerCase().indexOf(keyword.toLowerCase()) if (index > -1) { hit = true - const referText = c?.replaceAll(keyword, `${keyword}`) + const referText = c?.replace(re, `${keyword}`) post.results.push(`${referText}`) } else { post.results.push(`${c}`) From 2fbe4b736f23a7bc6ec8321cf0e15fff52e46c05 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 2 Mar 2022 10:32:18 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=85=A8=E6=96=87=E7=B4=A2=E5=BC=95?= =?UTF-8?q?=E5=8A=9F=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
{CONFIG_NEXT.MENU_SEARCH &&
- +
} From ac3ff4be61736bbec3dc6132cad94bc46022f0bb Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 2 Mar 2022 10:40:46 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E5=85=A8=E6=96=87=E7=B4=A2=E5=BC=95?= =?UTF-8?q?=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/NEXT/components/SearchInput.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/themes/NEXT/components/SearchInput.js b/themes/NEXT/components/SearchInput.js index f676a4fd..25dc8ccb 100644 --- a/themes/NEXT/components/SearchInput.js +++ b/themes/NEXT/components/SearchInput.js @@ -18,9 +18,10 @@ const SearchInput = ({ currentTag, currentSearch, cRef }) => { const handleSearch = (key) => { if (key && key !== '') { setLoadingState(true) - router.push({ pathname: '/search/' + key }).then(r => { - setLoadingState(false) - }) + // router.push({ pathname: '/search/' + key }).then(r => { + // setLoadingState(false) + // }) + location.href = '/search/' + key } else { router.push({ pathname: '/' }).then(r => { }) From f9ed6f1108834c491ec05b09498bba083d2a0453 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 2 Mar 2022 10:51:04 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E5=BC=80=E5=90=AF=E9=A2=84=E8=A7=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/NEXT/config_next.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/NEXT/config_next.js b/themes/NEXT/config_next.js index 92fa0743..393314e0 100644 --- a/themes/NEXT/config_next.js +++ b/themes/NEXT/config_next.js @@ -8,7 +8,7 @@ const CONFIG_NEXT = { POST_LIST_TYPE: 'page', // ['page','scroll] 文章列表样式:页码分页、单页滚动加载 POST_LIST_COVER: false, // 文章列表显示封面图 - POST_LIST_PREVIEW: false, // 显示文章预览 + POST_LIST_PREVIEW: true, // 显示文章预览 POST_LIST_SUMMARY: false, // 显示用户自定义摘要,有预览时优先只展示预览 // 右侧组件 From 0fe37565957f2bcf5358197ea45b7a90b74393fd Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 2 Mar 2022 10:51:44 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/search/[keyword].js | 1 + 1 file changed, 1 insertion(+) diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index 8463fc91..01f3a46b 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -64,6 +64,7 @@ export async function getStaticProps ({ params: { keyword } }) { const tagContent = post.tags ? post.tags.join(' ') : '' const categoryContent = post.category ? post.category.join(' ') : '' let indexContent = [post.title, post.summary, tagContent, categoryContent] + console.log('搜索是否命中缓存', page !== null) if (page !== null) { const contentIds = Object.keys(page.block) contentIds.forEach(id => { From 5c9b8377a8a80ace1bf526c11a0b9d0110db4714 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 2 Mar 2022 11:20:45 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/NEXT/components/SearchInput.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/themes/NEXT/components/SearchInput.js b/themes/NEXT/components/SearchInput.js index 25dc8ccb..73aa521b 100644 --- a/themes/NEXT/components/SearchInput.js +++ b/themes/NEXT/components/SearchInput.js @@ -4,7 +4,7 @@ import { useImperativeHandle, useRef, useState } from 'react' const SearchInput = ({ currentTag, currentSearch, cRef }) => { const { locale } = useGlobal() - const [searchKey, setSearchKey] = useState(currentSearch || '') + // const [searchKey, setSearchKey] = useState(currentSearch || '') const [onLoading, setLoadingState] = useState(false) const router = useRouter() const searchInputRef = useRef() @@ -36,11 +36,10 @@ const SearchInput = ({ currentTag, currentSearch, cRef }) => { } const cleanSearch = () => { searchInputRef.current.value = '' - setSearchKey('') } const updateSearchKey = (val) => { - setSearchKey(val) + searchInputRef.current.value = val } return
@@ -51,12 +50,12 @@ const SearchInput = ({ currentTag, currentSearch, cRef }) => { className={'w-full text-sm pl-4 transition focus:shadow-lg font-light leading-10 border-gray-300 text-black bg-gray-100 dark:bg-gray-900 dark:text-white'} onKeyUp={handleKeyUp} onChange={e => updateSearchKey(e.target.value)} - defaultValue={searchKey} + defaultValue={currentSearch} /> - {(searchKey && searchKey.length && )} + {(currentSearch && currentSearch.length > 0 && )}
{ handleSearch(searchKey) }}> + onClick={handleSearch}>
From 499729c0191f590c4d5ddbe40cded63f10eee071 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 2 Mar 2022 11:28:42 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A1=86=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/NEXT/components/SearchInput.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/themes/NEXT/components/SearchInput.js b/themes/NEXT/components/SearchInput.js index 73aa521b..2b4e88be 100644 --- a/themes/NEXT/components/SearchInput.js +++ b/themes/NEXT/components/SearchInput.js @@ -36,10 +36,17 @@ const SearchInput = ({ currentTag, currentSearch, cRef }) => { } const cleanSearch = () => { searchInputRef.current.value = '' + setShowClean(false) } + const [showClean, setShowClean] = useState(false) const updateSearchKey = (val) => { searchInputRef.current.value = val + if (val) { + setShowClean(true) + } else { + setShowClean(false) + } } return
@@ -52,7 +59,7 @@ const SearchInput = ({ currentTag, currentSearch, cRef }) => { onChange={e => updateSearchKey(e.target.value)} defaultValue={currentSearch} /> - {(currentSearch && currentSearch.length > 0 && )} + {(showClean && )}