From f8258a1de792cdaec8c493d62f77a3b37d5e9d19 Mon Sep 17 00:00:00 2001 From: anime Date: Mon, 23 Dec 2024 20:22:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E5=8F=AF=E8=AF=BB=E6=80=A7):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 304107fff915f037326b87cb78629a2cdcd9a5e4) --- lib/utils/post.js | 62 ++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/lib/utils/post.js b/lib/utils/post.js index be16cc1d..a3ee0f91 100644 --- a/lib/utils/post.js +++ b/lib/utils/post.js @@ -98,6 +98,38 @@ export function checkSlugHasMorThanTwoSlash(row) { ) } +/** + * 获取文章摘要 + * @param props + * @param pageContentText + * @returns {Promise} + */ +async function getPageAISummary(props, pageContentText) { + 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 += pageContentText + 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 + } + } +} + /** * 处理文章数据 * @param props @@ -110,8 +142,8 @@ export async function processPostData(props, from) { 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 ) @@ -120,33 +152,7 @@ export async function processPostData(props, from) { const { wordCount, readTime } = countWords(pageContentText) props.post.wordCount = wordCount props.post.readTime = readTime - 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 += pageContentText - 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 - } - } + await getPageAISummary(props, pageContentText) } // 生成全文索引 && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA)