mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
feat(统一文章不同路径结构下的数据处理方式): 使得其他路径结构下的文章也能进行原生AI摘要处理
(cherry picked from commit f427696b37872db96de53a071818a3fa98c691cd)
This commit is contained in:
@@ -2,6 +2,14 @@
|
||||
* 文章相关工具
|
||||
*/
|
||||
import { checkStartWithHttp } from '.'
|
||||
import { getPostBlocks } from '@/lib/db/getSiteData'
|
||||
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
|
||||
import { siteConfig } from '@/lib/config'
|
||||
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
|
||||
import { getPageContentText } from '@/pages/search/[keyword]'
|
||||
import { getAiSummary } from '@/lib/plugins/aiSummary'
|
||||
import BLOG from '@/blog.config'
|
||||
import { uploadDataToAlgolia } from '@/lib/plugins/algolia'
|
||||
|
||||
/**
|
||||
* 获取文章的关联推荐文章列表,目前根据标签关联性筛选
|
||||
@@ -88,3 +96,78 @@ export function checkSlugHasMorThanTwoSlash(row) {
|
||||
!checkStartWithHttp(slug)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理文章数据
|
||||
* @param props
|
||||
* @param from
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function processPostData(props, from) {
|
||||
// 文章内容加载
|
||||
if (!props?.post?.blockMap) {
|
||||
props.post.blockMap = await getPostBlocks(props.post.id, from)
|
||||
}
|
||||
|
||||
// 目录默认加载
|
||||
if (props.post?.blockMap?.block) {
|
||||
props.post.content = Object.keys(props.post.blockMap.block).filter(
|
||||
key => props.post.blockMap.block[key]?.value?.parent_id === props.post.id
|
||||
)
|
||||
props.post.toc = getPageTableOfContents(props.post, props.post.blockMap)
|
||||
|
||||
const aiSummaryAPI = siteConfig('AI_SUMMARY_API')
|
||||
if (aiSummaryAPI) {
|
||||
const post = props.post
|
||||
const cacheKey = `ai_summary_${post.id}`
|
||||
let aiSummary = await getDataFromCache(cacheKey)
|
||||
if (aiSummary) {
|
||||
props.post.aiSummary = aiSummary
|
||||
} else {
|
||||
const aiSummaryKey = siteConfig('AI_SUMMARY_KEY')
|
||||
const aiSummaryCacheTime = siteConfig('AI_SUMMARY_CACHE_TIME')
|
||||
const wordLimit = siteConfig('AI_SUMMARY_WORD_LIMIT', '1000')
|
||||
let content = ''
|
||||
for (let heading of post.toc) {
|
||||
content += heading.text + ' '
|
||||
}
|
||||
content += getPageContentText(post, post.blockMap)
|
||||
const combinedText = post.title + ' ' + content
|
||||
const truncatedText = combinedText.slice(0, wordLimit)
|
||||
aiSummary = await getAiSummary(
|
||||
aiSummaryAPI,
|
||||
aiSummaryKey,
|
||||
truncatedText
|
||||
)
|
||||
await setDataToCache(cacheKey, aiSummary, aiSummaryCacheTime)
|
||||
props.post.aiSummary = aiSummary
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 生成全文索引 && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA)
|
||||
if (BLOG.ALGOLIA_APP_ID) {
|
||||
uploadDataToAlgolia(props?.post)
|
||||
}
|
||||
|
||||
// 推荐关联文章处理
|
||||
const allPosts = props.allPages?.filter(
|
||||
page => page.type === 'Post' && page.status === 'Published'
|
||||
)
|
||||
if (allPosts && allPosts.length > 0) {
|
||||
const index = allPosts.indexOf(props.post)
|
||||
props.prev = allPosts.slice(index - 1, index)[0] ?? allPosts.slice(-1)[0]
|
||||
props.next = allPosts.slice(index + 1, index + 2)[0] ?? allPosts[0]
|
||||
props.recommendPosts = getRecommendPost(
|
||||
props.post,
|
||||
allPosts,
|
||||
siteConfig('POST_RECOMMEND_COUNT')
|
||||
)
|
||||
} else {
|
||||
props.prev = null
|
||||
props.next = null
|
||||
props.recommendPosts = []
|
||||
}
|
||||
|
||||
delete props.allPages
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user