From f053f42c21076a679d0e4488f89609be8abcc12b Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Tue, 1 Mar 2022 16:23:09 +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/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'