mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 15:09:22 +00:00
内存全文索引
This commit is contained in:
@@ -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, `<span class='text-red-500'>${keyword}</span>`)
|
||||
post.results.push(`<span>${referText}</span>`)
|
||||
}
|
||||
indexContent.forEach(c => {
|
||||
const index = c.indexOf(keyword)
|
||||
if (index > -1) {
|
||||
filterPosts.push(post)
|
||||
const referText = c?.replaceAll(keyword, `<span class='text-red-500'>${keyword}</span>`)
|
||||
post.results.push(`<span>${referText}</span>`)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
console.log(filterPosts)
|
||||
|
||||
return {
|
||||
props: {
|
||||
posts: filterPosts,
|
||||
|
||||
@@ -23,7 +23,7 @@ export const Layout404 = (props) => {
|
||||
<div
|
||||
className='md:-mt-20 text-black w-full h-screen text-center justify-center content-center items-center flex flex-col'>
|
||||
<div className='dark:text-gray-200'>
|
||||
<h2 className='inline-block border-r-2 border-gray-600 mr-2 px-3 py-2 align-top'><i className='mr-2 fa-spinner animate-spin'/>404</h2>
|
||||
<h2 className='inline-block border-r-2 border-gray-600 mr-2 px-3 py-2 align-top'><i className='mr-2 fas fa-spinner animate-spin'/>404</h2>
|
||||
<div className='inline-block text-left h-32 leading-10 items-center'>
|
||||
<h2 className='m-0 p-0'>页面无法加载,即将返回首页</h2>
|
||||
</div>
|
||||
|
||||
@@ -42,10 +42,10 @@ const BlogPostCard = ({ post, showSummary }) => {
|
||||
</div>
|
||||
|
||||
{(!showPreview || showSummary) && <p className='mt-4 mb-24 text-gray-700 dark:text-gray-300 text-sm font-light leading-7'>
|
||||
{post.summary}
|
||||
{post.summary && !post.results}
|
||||
{/* 搜索结果 */}
|
||||
{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>)}
|
||||
{post.results.map(r => <span key={r}><span dangerouslySetInnerHTML={{ __html: r }}/>...</span>)}
|
||||
</span>
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user