diff --git a/lib/utils/post.js b/lib/utils/post.js index e6402a02..efcb2904 100644 --- a/lib/utils/post.js +++ b/lib/utils/post.js @@ -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} + */ +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 +} diff --git a/pages/[prefix]/[slug]/[...suffix].js b/pages/[prefix]/[slug]/[...suffix].js index 90ca5a15..5e55ef85 100644 --- a/pages/[prefix]/[slug]/[...suffix].js +++ b/pages/[prefix]/[slug]/[...suffix].js @@ -1,9 +1,7 @@ import BLOG from '@/blog.config' import { siteConfig } from '@/lib/config' -import { getGlobalData, getPost, getPostBlocks } from '@/lib/db/getSiteData' -import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents' -import { uploadDataToAlgolia } from '@/lib/plugins/algolia' -import { checkSlugHasMorThanTwoSlash, getRecommendPost } from '@/lib/utils/post' +import { getGlobalData, getPost } from '@/lib/db/getSiteData' +import { checkSlugHasMorThanTwoSlash, processPostData } from '@/lib/utils/post' import { idToUuid } from 'notion-utils' import Slug from '..' @@ -79,59 +77,12 @@ export async function getStaticProps({ } } - // 无法获取文章 if (!props?.post) { + // 无法获取文章 props.post = null - return { - props, - revalidate: process.env.EXPORT - ? undefined - : siteConfig( - 'NEXT_REVALIDATE_SECOND', - BLOG.NEXT_REVALIDATE_SECOND, - props.NOTION_CONFIG - ) - } - } - - // 文章内容加载 - 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) - } - - // 生成全文索引 && 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 = [] + await processPostData(props, from) } - - delete props.allPages return { props, revalidate: process.env.EXPORT diff --git a/pages/[prefix]/[slug]/index.js b/pages/[prefix]/[slug]/index.js index fa9ced8e..cb81cfd6 100644 --- a/pages/[prefix]/[slug]/index.js +++ b/pages/[prefix]/[slug]/index.js @@ -1,14 +1,9 @@ import BLOG from '@/blog.config' import { siteConfig } from '@/lib/config' -import { getGlobalData, getPost, getPostBlocks } from '@/lib/db/getSiteData' -import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents' -import { uploadDataToAlgolia } from '@/lib/plugins/algolia' -import { checkSlugHasOneSlash, getRecommendPost } from '@/lib/utils/post' +import { getGlobalData, getPost } from '@/lib/db/getSiteData' +import { checkSlugHasOneSlash, processPostData } from '@/lib/utils/post' import { idToUuid } from 'notion-utils' import Slug from '..' -import { getPageContentText } from '@/pages/search/[keyword]' -import { getAiSummary } from '@/lib/plugins/aiSummary' -import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager' /** * 根据notion的slug访问页面 @@ -71,87 +66,12 @@ export async function getStaticProps({ params: { prefix, slug }, locale }) { } } - // 无法获取文章 if (!props?.post) { + // 无法获取文章 props.post = null - return { - props, - revalidate: process.env.EXPORT - ? undefined - : siteConfig( - 'NEXT_REVALIDATE_SECOND', - BLOG.NEXT_REVALIDATE_SECOND, - props.NOTION_CONFIG - ) - } - } - - // 文章内容加载 - 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 = [] + await processPostData(props, from) } - - delete props.allPages return { props, revalidate: process.env.EXPORT diff --git a/pages/[prefix]/index.js b/pages/[prefix]/index.js index e26a95a9..672e6a0d 100644 --- a/pages/[prefix]/index.js +++ b/pages/[prefix]/index.js @@ -2,12 +2,11 @@ import BLOG from '@/blog.config' import useNotification from '@/components/Notification' import OpenWrite from '@/components/OpenWrite' import { siteConfig } from '@/lib/config' -import { getGlobalData, getPost, getPostBlocks } from '@/lib/db/getSiteData' +import { getGlobalData, getPost } from '@/lib/db/getSiteData' import { useGlobal } from '@/lib/global' import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents' import { getPasswordQuery } from '@/lib/password' -import { uploadDataToAlgolia } from '@/lib/plugins/algolia' -import { checkSlugHasNoSlash, getRecommendPost } from '@/lib/utils/post' +import { checkSlugHasNoSlash, processPostData } from '@/lib/utils/post' import { DynamicLayout } from '@/themes/theme' import md5 from 'js-md5' import { useRouter } from 'next/router' @@ -141,59 +140,12 @@ export async function getStaticProps({ params: { prefix }, locale }) { props.post = post } } - // 无法获取文章 if (!props?.post) { + // 无法获取文章 props.post = null - return { - props, - revalidate: process.env.EXPORT - ? undefined - : siteConfig( - 'NEXT_REVALIDATE_SECOND', - BLOG.NEXT_REVALIDATE_SECOND, - props.NOTION_CONFIG - ) - } - } - - // 文章内容加载 - 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) - } - - // 生成全文索引 && process.env.npm_lifecycle_event === 'build' && 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 = [] + await processPostData(props, from) } - - delete props.allPages return { props, revalidate: process.env.EXPORT