内存全文索引

This commit is contained in:
tangly1024
2022-03-01 16:23:09 +08:00
parent 26ac377749
commit f053f42c21
8 changed files with 108 additions and 6 deletions

View File

@@ -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)

View File

@@ -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)

98
pages/search/[keyword].js Normal file
View File

@@ -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, `<span class='text-red-500'>${keyword}</span>`)
post.results.push(`<span>${referText}</span>`)
}
})
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 <LayoutSearch {...props} meta={meta} currentSearch={keyword} />
}
export default Index

View File

@@ -43,6 +43,12 @@ const BlogPostCard = ({ post, showSummary }) => {
{(!showPreview || showSummary) && <p className='mt-4 mb-24 text-gray-700 dark:text-gray-300 text-sm font-light leading-7'>
{post.summary}
{/* 搜索结果 */}
{post.results && <span className='mt-4 text-gray-700 dark:text-gray-300 text-sm font-light leading-7'>
{post.results.map(r => <span key={r}>...<span dangerouslySetInnerHTML={{ __html: r }}/>...</span>)}
</span>
}
</p>}
{showPreview && post?.blockMap && <div className='overflow-ellipsis truncate'>

View File

@@ -65,7 +65,7 @@ const BlogPostListScroll = ({ posts = [], currentSearch, showSummary = CONFIG_NE
<div onClick={() => {
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} 😰`} </div>
</div>
</div>

View File

@@ -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 {

View File

@@ -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'