Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf/all posts 优化数据生成过程 #3224

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions lib/db/getSiteData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -302,7 +309,8 @@ async function convertNotionToSiteDate(pageId, from, pageRecordMap) {
rawMetadata,
customNav,
customMenu,
postCount,
allPublishedPosts,
postCount: allPublishedPosts.length,
pageIds,
latestPosts
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 => ({
Expand Down
15 changes: 7 additions & 8 deletions lib/notion/getAllCategories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@ import { isIterable } from '../utils'

/**
* 获取所有文章的标签
* @param allPosts
* @param allPublishedPosts
* @param sliceCount 默认截取数量为12,若为0则返回全部
* @param categoryOptions categories的下拉选项
* @returns {Promise<{}|*[]>}
*/

/**
* 获取所有文章的分类
* @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 => {
Expand Down
13 changes: 5 additions & 8 deletions lib/notion/getAllTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
20 changes: 10 additions & 10 deletions lib/utils/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pages/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
10 changes: 5 additions & 5 deletions pages/search/[keyword]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 =
Expand Down
10 changes: 5 additions & 5 deletions pages/search/[keyword]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
// 处理分页
Expand Down Expand Up @@ -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 =
Expand Down