algolia 搜索相关

This commit is contained in:
tangly1024.com
2023-07-12 15:15:32 +08:00
parent 0b7c79cef2
commit aa5373d2ff
8 changed files with 62 additions and 15 deletions

View File

@@ -9,6 +9,7 @@ import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
import { getLayoutByTheme } from '@/themes/theme'
import md5 from 'js-md5'
import { isBrowser } from '@/lib/utils'
import { uploadDataToAlgolia } from '@/lib/algolia'
/**
* 根据notion的slug访问页面
@@ -128,6 +129,10 @@ export async function getStaticProps({ params: { slug } }) {
props.post.blockMap = await getPostBlocks(props.post.id, from)
}
if (BLOG.ALGOLIA_APP_ID && BLOG.ALGOLIA_APP_KEY) {
uploadDataToAlgolia(props?.post)
}
// 推荐关联文章处理
const allPosts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published')
if (allPosts && allPosts.length > 0) {

View File

@@ -6,6 +6,7 @@ import { generateRobotsTxt } from '@/lib/robots.txt'
import { useRouter } from 'next/router'
import { getLayoutByTheme } from '@/themes/theme'
/**
* 首页布局
* @param {*} props

View File

@@ -121,16 +121,7 @@ async function filterByMemCache(allPosts, keyword) {
const categoryContent = post.category && Array.isArray(post.category) ? post.category.join(' ') : ''
const articleInfo = post.title + post.summary + tagContent + categoryContent
let hit = articleInfo.toLowerCase().indexOf(keyword) > -1
let indexContent = [post.summary]
// 防止搜到加密文章的内容
if (page && page.block && !post.password) {
const contentIds = Object.keys(page.block)
contentIds.forEach(id => {
const properties = page?.block[id]?.value?.properties
indexContent = appendText(indexContent, properties, 'title')
indexContent = appendText(indexContent, properties, 'caption')
})
}
const indexContent = getPageContentText(post, page)
// console.log('全文搜索缓存', cacheKey, page != null)
post.results = []
let hitCount = 0
@@ -157,4 +148,18 @@ async function filterByMemCache(allPosts, keyword) {
return filterPosts
}
export function getPageContentText(post, pageBlockMap) {
let indexContent = []
// 防止搜到加密文章的内容
if (pageBlockMap && pageBlockMap.block && !post.password) {
const contentIds = Object.keys(pageBlockMap.block)
contentIds.forEach(id => {
const properties = pageBlockMap?.block[id]?.value?.properties
indexContent = appendText(indexContent, properties, 'title')
indexContent = appendText(indexContent, properties, 'caption')
})
}
return indexContent.join('')
}
export default Index