diff --git a/lib/db/getSiteData.js b/lib/db/getSiteData.js index 6d9c2c66736..aa21ae659d8 100755 --- a/lib/db/getSiteData.js +++ b/lib/db/getSiteData.js @@ -227,13 +227,13 @@ async function convertNotionToSiteDate(pageId, from, pageRecordMap) { // 站点基础信息 const siteInfo = getSiteInfo({ collection, block, NOTION_CONFIG }) - // 文章计数 - let postCount = 0 + // 所有已发布文章 + const allPublishedPosts = [] // 查找所有的Post和Page const allPages = collectionData.filter(post => { if (post?.type === 'Post' && post.status === 'Published') { - postCount++ + allPublishedPosts.push(post) } return ( @@ -249,6 +249,9 @@ async function convertNotionToSiteDate(pageId, from, pageRecordMap) { allPages.sort((a, b) => { return b?.publishDate - a?.publishDate }) + allPublishedPosts.sort((a, b) => { + return b?.publishDate - a?.publishDate + }) } const notice = await getNotice( @@ -263,13 +266,13 @@ async function convertNotionToSiteDate(pageId, from, pageRecordMap) { ) // 所有分类 const categoryOptions = getAllCategories({ - allPages, + allPublishedPosts, categoryOptions: getCategoryOptions(schema) }) // 所有标签 const tagOptions = getAllTags({ - allPages, + allPublishedPosts, tagOptions: getTagOptions(schema), NOTION_CONFIG }) || null @@ -281,8 +284,12 @@ async function convertNotionToSiteDate(pageId, from, pageRecordMap) { }) // 新的菜单 const customMenu = await getCustomMenu({ collectionData, NOTION_CONFIG }) - const latestPosts = getLatestPosts({ allPages, from, latestPostCount: 6 }) - const allNavPages = getNavPages({ allPages }) + const latestPosts = getLatestPosts({ + allPublishedPosts, + from, + latestPostCount: 6 + }) + const allNavPages = getNavPages({ allPublishedPosts }) return { NOTION_CONFIG, @@ -302,7 +309,8 @@ async function convertNotionToSiteDate(pageId, from, pageRecordMap) { rawMetadata, customNav, customMenu, - postCount, + allPublishedPosts, + postCount: allPublishedPosts.length, pageIds, latestPosts } @@ -502,15 +510,13 @@ function cleanBlock(item) { /** * 获取最新文章 根据最后修改时间倒序排列 - * @param {*}} param0 + * @param {*} allPublishedPosts + * @param from + * @param latestPostCount * @returns */ -function getLatestPosts({ allPages, from, latestPostCount }) { - const allPosts = allPages?.filter( - page => page.type === 'Post' && page.status === 'Published' - ) - - const latestPosts = Object.create(allPosts).sort((a, b) => { +function getLatestPosts({ allPublishedPosts, from, latestPostCount }) { + const latestPosts = Object.create(allPublishedPosts).sort((a, b) => { const dateA = new Date(a?.lastEditedDate || a?.publishDate) const dateB = new Date(b?.lastEditedDate || b?.publishDate) return dateB - dateA @@ -797,16 +803,11 @@ function getTimestamp(date, time = '00:00', time_zone) { * 获取导航用的精减文章列表 * gitbook主题用到,只保留文章的标题分类标签分类信息,精减掉摘要密码日期等数据 * 导航页面的条件,必须是Posts - * @param {*} param0 + * @param {*} allPublishedPosts */ -export function getNavPages({ allPages }) { - const allNavPages = allPages?.filter(post => { - return ( - post && - post?.slug && - post?.type === 'Post' && - post?.status === 'Published' - ) +export function getNavPages({ allPublishedPosts }) { + const allNavPages = allPublishedPosts?.filter(post => { + return post?.slug }) return allNavPages.map(item => ({ diff --git a/lib/notion/getAllCategories.js b/lib/notion/getAllCategories.js index c7e66ba2252..831aedb3784 100644 --- a/lib/notion/getAllCategories.js +++ b/lib/notion/getAllCategories.js @@ -2,7 +2,7 @@ import { isIterable } from '../utils' /** * 获取所有文章的标签 - * @param allPosts + * @param allPublishedPosts * @param sliceCount 默认截取数量为12,若为0则返回全部 * @param categoryOptions categories的下拉选项 * @returns {Promise<{}|*[]>} @@ -10,22 +10,21 @@ import { isIterable } from '../utils' /** * 获取所有文章的分类 - * @param allPosts + * @param allPublishedPosts + * @param categoryOptions + * @param sliceCount * @returns {Promise<{}|*[]>} */ export function getAllCategories({ - allPages, + allPublishedPosts, categoryOptions, sliceCount = 0 }) { - const allPosts = allPages?.filter( - page => page.type === 'Post' && page.status === 'Published' - ) - if (!allPosts || !categoryOptions) { + if (!allPublishedPosts || !categoryOptions) { return [] } // 计数 - let categories = allPosts?.map(p => p.category) + let categories = allPublishedPosts?.map(p => p.category) categories = [...categories.flat()] const categoryObj = {} categories.forEach(category => { diff --git a/lib/notion/getAllTags.js b/lib/notion/getAllTags.js index cac8dac75e9..7298708fd4d 100644 --- a/lib/notion/getAllTags.js +++ b/lib/notion/getAllTags.js @@ -3,26 +3,23 @@ import { isIterable } from '../utils' /** * 获取所有文章的标签 - * @param allPosts + * @param allPublishedPosts * @param sliceCount 默认截取数量为12,若为0则返回全部 * @param tagOptions tags的下拉选项 + * @param NOTION_CONFIG * @returns {Promise<{}|*[]>} */ export function getAllTags({ - allPages, + allPublishedPosts, sliceCount = 0, tagOptions, NOTION_CONFIG }) { - const allPosts = allPages?.filter( - page => page.type === 'Post' && page.status === 'Published' - ) - - if (!allPosts || !tagOptions) { + if (!allPublishedPosts || !tagOptions) { return [] } // 计数 - let tags = allPosts?.map(p => p.tags) + let tags = allPublishedPosts?.map(p => p.tags) tags = [...tags.flat()] const tagObj = {} tags.forEach(tag => { diff --git a/lib/utils/post.js b/lib/utils/post.js index a3ee0f9141c..128f44e2359 100644 --- a/lib/utils/post.js +++ b/lib/utils/post.js @@ -15,16 +15,16 @@ import { countWords } from '@/lib/plugins/wordCount' /** * 获取文章的关联推荐文章列表,目前根据标签关联性筛选 * @param post - * @param {*} allPosts + * @param {*} allPublishedPosts * @param {*} count * @returns */ -export function getRecommendPost(post, allPosts, count = 6) { +export function getRecommendPost(post, allPublishedPosts, count = 6) { let recommendPosts = [] const postIds = [] const currentTags = post?.tags || [] - for (let i = 0; i < allPosts.length; i++) { - const p = allPosts[i] + for (let i = 0; i < allPublishedPosts.length; i++) { + const p = allPublishedPosts[i] if (p.id === post.id || p.type.indexOf('Post') < 0) { continue } @@ -161,16 +161,16 @@ export async function processPostData(props, from) { } // 推荐关联文章处理 - const allPosts = props.allPages?.filter( + const allPublishedPosts = 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] + if (allPublishedPosts && allPublishedPosts.length > 0) { + const index = allPublishedPosts.indexOf(props.post) + props.prev = allPublishedPosts.slice(index - 1, index)[0] ?? allPublishedPosts.slice(-1)[0] + props.next = allPublishedPosts.slice(index + 1, index + 2)[0] ?? allPublishedPosts[0] props.recommendPosts = getRecommendPost( props.post, - allPosts, + allPublishedPosts, siteConfig('POST_RECOMMEND_COUNT') ) } else { diff --git a/pages/page/[page].js b/pages/page/[page].js index 1ee22afb574..6451112ce22 100644 --- a/pages/page/[page].js +++ b/pages/page/[page].js @@ -38,12 +38,12 @@ export async function getStaticProps({ params: { page }, locale }) { props?.NOTION_CONFIG ) - const allPosts = allPages?.filter( + const allPublishedPosts = allPages?.filter( page => page.type === 'Post' && page.status === 'Published' ) const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG) // 处理分页 - props.posts = allPosts.slice( + props.posts = allPublishedPosts.slice( POSTS_PER_PAGE * (page - 1), POSTS_PER_PAGE * page ) diff --git a/pages/search/[keyword]/index.js b/pages/search/[keyword]/index.js index b62ef8a9cd7..d8fbfd9b197 100644 --- a/pages/search/[keyword]/index.js +++ b/pages/search/[keyword]/index.js @@ -20,10 +20,10 @@ export async function getStaticProps({ params: { keyword }, locale }) { locale }) const { allPages } = props - const allPosts = allPages?.filter( + const allPublishedPosts = allPages?.filter( page => page.type === 'Post' && page.status === 'Published' ) - props.posts = await filterByMemCache(allPosts, keyword) + props.posts = await filterByMemCache(allPublishedPosts, keyword) props.postCount = props.posts.length const POST_LIST_STYLE = siteConfig( 'POST_LIST_STYLE', @@ -104,16 +104,16 @@ const isIterable = obj => /** * 在内存缓存中进行全文索引 - * @param {*} allPosts + * @param {*} allPublishedPosts * @param keyword 关键词 * @returns */ -async function filterByMemCache(allPosts, keyword) { +async function filterByMemCache(allPublishedPosts, keyword) { const filterPosts = [] if (keyword) { keyword = keyword.trim().toLowerCase() } - for (const post of allPosts) { + for (const post of allPublishedPosts) { const cacheKey = 'page_block_' + post.id const page = await getDataFromCache(cacheKey, true) const tagContent = diff --git a/pages/search/[keyword]/page/[page].js b/pages/search/[keyword]/page/[page].js index e0d20acc36a..57070cd63ae 100644 --- a/pages/search/[keyword]/page/[page].js +++ b/pages/search/[keyword]/page/[page].js @@ -24,10 +24,10 @@ export async function getStaticProps({ params: { keyword, page }, locale }) { locale }) const { allPages } = props - const allPosts = allPages?.filter( + const allPublishedPosts = allPages?.filter( page => page.type === 'Post' && page.status === 'Published' ) - props.posts = await filterByMemCache(allPosts, keyword) + props.posts = await filterByMemCache(allPublishedPosts, keyword) props.postCount = props.posts.length const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG) // 处理分页 @@ -103,16 +103,16 @@ const isIterable = obj => /** * 在内存缓存中进行全文索引 - * @param {*} allPosts + * @param {*} allPublishedPosts * @param keyword 关键词 * @returns */ -async function filterByMemCache(allPosts, keyword) { +async function filterByMemCache(allPublishedPosts, keyword) { const filterPosts = [] if (keyword) { keyword = keyword.trim() } - for (const post of allPosts) { + for (const post of allPublishedPosts) { const cacheKey = 'page_block_' + post.id const page = await getDataFromCache(cacheKey, true) const tagContent =