Merge pull request #3482 from qixing-jk/feat/algolia/delete-post

feat(algolia): add data deletion functionality and execute in index.js
This commit is contained in:
tangly1024
2025-07-27 21:21:27 +08:00
committed by GitHub
2 changed files with 76 additions and 8 deletions

View File

@@ -2,6 +2,31 @@ import BLOG from '@/blog.config'
import { getPageContentText } from '@/pages/search/[keyword]'
import algoliasearch from 'algoliasearch'
// 全局初始化 Algolia 客户端和索引
let algoliaClient
let algoliaIndex
const initAlgolia = () => {
if (!algoliaClient) {
if (
!BLOG.ALGOLIA_APP_ID ||
!BLOG.ALGOLIA_ADMIN_APP_KEY ||
!BLOG.ALGOLIA_INDEX
) {
console.error('Algolia configuration is missing')
}
algoliaClient = algoliasearch(
BLOG.ALGOLIA_APP_ID,
BLOG.ALGOLIA_ADMIN_APP_KEY
)
algoliaIndex = algoliaClient.initIndex(BLOG.ALGOLIA_INDEX)
}
return { client: algoliaClient, index: algoliaIndex }
}
// 初始化全局实例
initAlgolia()
/**
* 生成全文索引
* @param {*} allPages
@@ -15,17 +40,57 @@ const generateAlgoliaSearch = ({ allPages, force = false }) => {
})
}
/**
* 检查数据是否需要从algolia删除
* @param {*} props
*/
export const checkDataFromAlgolia = async props => {
const { allPages } = props
const deletions = (allPages || [])
.map(p => {
if (p && (p.password || p.status === 'Draft')) {
return deletePostDataFromAlgolia(p)
}
})
.filter(Boolean) // 去除 undefined
await Promise.all(deletions)
}
/**
* 删除数据
* @param post
*/
const deletePostDataFromAlgolia = async post => {
if (!post) {
return
}
// 检查是否有索引
let existed
try {
existed = await algoliaIndex.getObject(post.id)
} catch (error) {
// 通常是不存在索引
}
if (existed) {
await algoliaIndex
.deleteObject(post.id)
.wait()
.then(r => {
console.log('Algolia索引删除成功', r)
})
.catch(err => {
console.log('Algolia异常', err)
})
}
}
/**
* 上传数据
* 根据上次修改文章日期和上次更新索引数据判断是否需要更新algolia索引
*/
const uploadDataToAlgolia = async post => {
// Connect and authenticate with your Algolia app
const client = algoliasearch(BLOG.ALGOLIA_APP_ID, BLOG.ALGOLIA_ADMIN_APP_KEY)
// Create a new index and add a record
const index = client.initIndex(BLOG.ALGOLIA_INDEX)
if (!post) {
return
}
@@ -34,7 +99,7 @@ const uploadDataToAlgolia = async post => {
let existed
let needUpdateIndex = false
try {
existed = await index.getObject(post.id)
existed = await algoliaIndex.getObject(post.id)
} catch (error) {
// 通常是不存在索引
}
@@ -64,7 +129,7 @@ const uploadDataToAlgolia = async post => {
content: truncate(getPageContentText(post, post.blockMap), 8192) // 索引8192个字符API限制总请求内容上限1万个字节
}
// console.log('更新Algolia索引', record)
index
algoliaIndex
.saveObject(record)
.wait()
.then(r => {

View File

@@ -6,6 +6,7 @@ import { generateRss } from '@/lib/rss'
import { generateSitemapXml } from '@/lib/sitemap.xml'
import { DynamicLayout } from '@/themes/theme'
import { generateRedirectJson } from '@/lib/redirect'
import { checkDataFromAlgolia } from '@/lib/plugins/algolia'
/**
* 首页布局
@@ -61,6 +62,8 @@ export async function getStaticProps(req) {
generateRss(props)
// 生成
generateSitemapXml(props)
// 检查数据是否需要从algolia删除
checkDataFromAlgolia(props)
if (siteConfig('UUID_REDIRECT', false, props?.NOTION_CONFIG)) {
// 生成重定向 JSON
generateRedirectJson(props)