mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-01 23:16:51 +00:00
feat(统一文章不同路径结构下的数据处理方式): 使得其他路径结构下的文章也能进行原生AI摘要处理
(cherry picked from commit f427696b37872db96de53a071818a3fa98c691cd)
This commit is contained in:
@@ -2,6 +2,14 @@
|
|||||||
* 文章相关工具
|
* 文章相关工具
|
||||||
*/
|
*/
|
||||||
import { checkStartWithHttp } from '.'
|
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)
|
!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
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
import BLOG from '@/blog.config'
|
import BLOG from '@/blog.config'
|
||||||
import { siteConfig } from '@/lib/config'
|
import { siteConfig } from '@/lib/config'
|
||||||
import { getGlobalData, getPost, getPostBlocks } from '@/lib/db/getSiteData'
|
import { getGlobalData, getPost } from '@/lib/db/getSiteData'
|
||||||
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
|
import { checkSlugHasMorThanTwoSlash, processPostData } from '@/lib/utils/post'
|
||||||
import { uploadDataToAlgolia } from '@/lib/plugins/algolia'
|
|
||||||
import { checkSlugHasMorThanTwoSlash, getRecommendPost } from '@/lib/utils/post'
|
|
||||||
import { idToUuid } from 'notion-utils'
|
import { idToUuid } from 'notion-utils'
|
||||||
import Slug from '..'
|
import Slug from '..'
|
||||||
|
|
||||||
@@ -79,59 +77,12 @@ export async function getStaticProps({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 无法获取文章
|
|
||||||
if (!props?.post) {
|
if (!props?.post) {
|
||||||
|
// 无法获取文章
|
||||||
props.post = null
|
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 {
|
} else {
|
||||||
props.prev = null
|
await processPostData(props, from)
|
||||||
props.next = null
|
|
||||||
props.recommendPosts = []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete props.allPages
|
|
||||||
return {
|
return {
|
||||||
props,
|
props,
|
||||||
revalidate: process.env.EXPORT
|
revalidate: process.env.EXPORT
|
||||||
|
|||||||
@@ -1,14 +1,9 @@
|
|||||||
import BLOG from '@/blog.config'
|
import BLOG from '@/blog.config'
|
||||||
import { siteConfig } from '@/lib/config'
|
import { siteConfig } from '@/lib/config'
|
||||||
import { getGlobalData, getPost, getPostBlocks } from '@/lib/db/getSiteData'
|
import { getGlobalData, getPost } from '@/lib/db/getSiteData'
|
||||||
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
|
import { checkSlugHasOneSlash, processPostData } from '@/lib/utils/post'
|
||||||
import { uploadDataToAlgolia } from '@/lib/plugins/algolia'
|
|
||||||
import { checkSlugHasOneSlash, getRecommendPost } from '@/lib/utils/post'
|
|
||||||
import { idToUuid } from 'notion-utils'
|
import { idToUuid } from 'notion-utils'
|
||||||
import Slug from '..'
|
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访问页面
|
* 根据notion的slug访问页面
|
||||||
@@ -71,87 +66,12 @@ export async function getStaticProps({ params: { prefix, slug }, locale }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 无法获取文章
|
|
||||||
if (!props?.post) {
|
if (!props?.post) {
|
||||||
|
// 无法获取文章
|
||||||
props.post = null
|
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 {
|
} else {
|
||||||
props.prev = null
|
await processPostData(props, from)
|
||||||
props.next = null
|
|
||||||
props.recommendPosts = []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete props.allPages
|
|
||||||
return {
|
return {
|
||||||
props,
|
props,
|
||||||
revalidate: process.env.EXPORT
|
revalidate: process.env.EXPORT
|
||||||
|
|||||||
@@ -2,12 +2,11 @@ import BLOG from '@/blog.config'
|
|||||||
import useNotification from '@/components/Notification'
|
import useNotification from '@/components/Notification'
|
||||||
import OpenWrite from '@/components/OpenWrite'
|
import OpenWrite from '@/components/OpenWrite'
|
||||||
import { siteConfig } from '@/lib/config'
|
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 { useGlobal } from '@/lib/global'
|
||||||
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
|
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
|
||||||
import { getPasswordQuery } from '@/lib/password'
|
import { getPasswordQuery } from '@/lib/password'
|
||||||
import { uploadDataToAlgolia } from '@/lib/plugins/algolia'
|
import { checkSlugHasNoSlash, processPostData } from '@/lib/utils/post'
|
||||||
import { checkSlugHasNoSlash, getRecommendPost } from '@/lib/utils/post'
|
|
||||||
import { DynamicLayout } from '@/themes/theme'
|
import { DynamicLayout } from '@/themes/theme'
|
||||||
import md5 from 'js-md5'
|
import md5 from 'js-md5'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
@@ -141,59 +140,12 @@ export async function getStaticProps({ params: { prefix }, locale }) {
|
|||||||
props.post = post
|
props.post = post
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 无法获取文章
|
|
||||||
if (!props?.post) {
|
if (!props?.post) {
|
||||||
|
// 无法获取文章
|
||||||
props.post = null
|
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 {
|
} else {
|
||||||
props.prev = null
|
await processPostData(props, from)
|
||||||
props.next = null
|
|
||||||
props.recommendPosts = []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete props.allPages
|
|
||||||
return {
|
return {
|
||||||
props,
|
props,
|
||||||
revalidate: process.env.EXPORT
|
revalidate: process.env.EXPORT
|
||||||
|
|||||||
Reference in New Issue
Block a user