Merge pull request #73 from tangly1024/feature-search

Feature search
This commit is contained in:
tangly1024
2022-03-02 16:06:24 +08:00
committed by GitHub
2 changed files with 18 additions and 24 deletions

View File

@@ -2,14 +2,7 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { LayoutSearch } from '@/themes'
import BLOG from '@/blog.config'
import { useGlobal } from '@/lib/global'
import { getDataFromCache } from '@/lib/cache/cache_manager'
export async function getStaticPaths () {
return {
paths: [],
fallback: true
}
}
import { getPostBlocks } from '@/lib/notion/getPostBlocks'
/**
* 将对象的指定字段拼接到字符串
@@ -38,16 +31,16 @@ function appendText (sourceTextArray, targetObj, key) {
function getTextContent (textArray) {
if (typeof textArray === 'object') {
let result = ''
textArray.forEach(textObj => {
for (const textObj of textArray) {
result = result + getTextContent(textObj)
})
}
return result
} else if (typeof textArray === 'string') {
return textArray
}
}
export async function getStaticProps ({ params: { keyword } }) {
export async function getServerSideProps ({ params: { keyword } }) {
const {
allPosts,
categories,
@@ -59,14 +52,13 @@ export async function getStaticProps ({ params: { keyword } }) {
const filterPosts = []
for (const post of allPosts) {
const cacheKey = 'page_block_' + post.id
const page = await getDataFromCache(cacheKey)
// const cacheKey = 'page_block_' + post.id
const page = await getPostBlocks(post.id, 'search')
const tagContent = post.tags ? post.tags.join(' ') : ''
const categoryContent = post.category ? post.category.join(' ') : ''
const articleInfo = post.title + post.summary + tagContent + categoryContent
let hit = articleInfo.indexOf(keyword) > -1
let indexContent = [post.summary]
console.log('搜索是否命中缓存', page !== null)
if (page !== null) {
const contentIds = Object.keys(page.block)
contentIds.forEach(id => {
@@ -75,23 +67,25 @@ export async function getStaticProps ({ params: { keyword } }) {
indexContent = appendText(indexContent, properties, 'caption')
})
}
console.log('搜索是否命中缓存', page !== null, indexContent)
post.results = []
let hitCount = 0
const re = new RegExp(`${keyword}`, 'g')
indexContent.forEach((c, i) => {
const re = new RegExp(`${keyword}`, 'gim')
for (const i in indexContent) {
const c = indexContent[i]
const index = c.toLowerCase().indexOf(keyword.toLowerCase())
const referText = c?.replace(re, `<span class='text-red-500'>${keyword}</span>`)
if (index > -1) {
hit = true
const referText = c?.replace(re, `<span class='text-red-500'>${keyword}</span>`)
post.results.push(`<span>${referText}</span>`)
hitCount += 1
post.results.push(`<span>${referText}</span>`)
} else {
if ((post.results.length - 1) / hitCount < 3 || i === 0) {
post.results.push(`<span>${c}</span>`)
post.results.push(`<span>${referText}</span>`)
}
}
})
}
if (hit) {
filterPosts.push(post)
}
@@ -106,8 +100,7 @@ export async function getStaticProps ({ params: { keyword } }) {
latestPosts,
customNav,
keyword
},
revalidate: 1
}
}
}

View File

@@ -2,6 +2,8 @@ import { useRouter } from 'next/router'
import { useGlobal } from '@/lib/global'
import { useImperativeHandle, useRef, useState } from 'react'
let lock = false
const SearchInput = ({ currentTag, currentSearch, cRef }) => {
const { locale } = useGlobal()
// const [searchKey, setSearchKey] = useState(currentSearch || '')
@@ -39,7 +41,6 @@ const SearchInput = ({ currentTag, currentSearch, cRef }) => {
searchInputRef.current.value = ''
setShowClean(false)
}
let lock = false
function lockSearchInput () {
lock = true
}