mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-08 15:10:37 +00:00
封装 allPages
This commit is contained in:
@@ -1,3 +1,2 @@
|
|||||||
export { getAllPosts } from './notion/getAllPosts'
|
|
||||||
export { getAllTags } from './notion/getAllTags'
|
export { getAllTags } from './notion/getAllTags'
|
||||||
export { getPostBlocks } from './notion/getPostBlocks'
|
export { getPostBlocks } from './notion/getPostBlocks'
|
||||||
|
|||||||
@@ -1,59 +0,0 @@
|
|||||||
import BLOG from '@/blog.config'
|
|
||||||
import getAllPageIds from './getAllPageIds'
|
|
||||||
import getPageProperties from './getPageProperties'
|
|
||||||
import { getNotionPageData } from '@/lib/notion/getNotionData'
|
|
||||||
import { delCacheData } from '@/lib/cache/cache_manager'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取所有文章列表
|
|
||||||
* @param notionPageData
|
|
||||||
* @param from
|
|
||||||
* @param pageType 页面类型数组 ['Post','Page']
|
|
||||||
* @returns {Promise<*[]>}
|
|
||||||
*/
|
|
||||||
export async function getAllPosts({ notionPageData, from, pageType }) {
|
|
||||||
if (!notionPageData) {
|
|
||||||
notionPageData = await getNotionPageData({ from })
|
|
||||||
}
|
|
||||||
if (!notionPageData) {
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
|
|
||||||
const { block, schema, tagOptions, collectionQuery, collectionId, collectionView, viewIds } = notionPageData
|
|
||||||
const data = []
|
|
||||||
const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds)
|
|
||||||
for (let i = 0; i < pageIds.length; i++) {
|
|
||||||
const id = pageIds[i]
|
|
||||||
const value = block[id]?.value
|
|
||||||
if (!value) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
const properties = (await getPageProperties(id, block, schema, tagOptions)) || null
|
|
||||||
data.push(properties)
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove all the the items doesn't meet requirements
|
|
||||||
const posts = data.filter(post => {
|
|
||||||
if (pageType === 'Post') {
|
|
||||||
return post.title && post?.status?.[0] === 'Published' && pageType.indexOf(post?.type?.[0]) > -1
|
|
||||||
} else {
|
|
||||||
// 隐藏单页
|
|
||||||
return post.title && (post?.status?.[0] === 'Published' || post?.status?.[0] === 'Invisible') && pageType.indexOf(post?.type?.[0]) > -1
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!posts || posts.length === 0) {
|
|
||||||
const cacheKey = 'page_block_' + BLOG.NOTION_PAGE_ID
|
|
||||||
await delCacheData(cacheKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort by date
|
|
||||||
if (BLOG.POSTS_SORT_BY === 'date') {
|
|
||||||
posts.sort((a, b) => {
|
|
||||||
const dateA = new Date(a?.date?.start_date || a.createdTime)
|
|
||||||
const dateB = new Date(b?.date?.start_date || b.createdTime)
|
|
||||||
return dateB - dateA
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return posts
|
|
||||||
}
|
|
||||||
@@ -5,7 +5,6 @@ import { idToUuid } from 'notion-utils'
|
|||||||
import { defaultMapImageUrl } from 'react-notion-x'
|
import { defaultMapImageUrl } from 'react-notion-x'
|
||||||
import { deepClone, isIterable } from '../utils'
|
import { deepClone, isIterable } from '../utils'
|
||||||
import getAllPageIds from './getAllPageIds'
|
import getAllPageIds from './getAllPageIds'
|
||||||
import { getAllPosts } from './getAllPosts'
|
|
||||||
import { getAllTags } from './getAllTags'
|
import { getAllTags } from './getAllTags'
|
||||||
import getPageProperties from './getPageProperties'
|
import getPageProperties from './getPageProperties'
|
||||||
|
|
||||||
@@ -29,8 +28,6 @@ export async function getGlobalNotionData({
|
|||||||
const notionPageData = deepClone(await getNotionPageData({ pageId, from }))
|
const notionPageData = deepClone(await getNotionPageData({ pageId, from }))
|
||||||
notionPageData.siteInfo = getBlogInfo({ collection: notionPageData?.collection, block: notionPageData?.block })
|
notionPageData.siteInfo = getBlogInfo({ collection: notionPageData?.collection, block: notionPageData?.block })
|
||||||
|
|
||||||
// 获取文章列表
|
|
||||||
notionPageData.allPosts = await getAllPosts({ notionPageData, from, pageType })
|
|
||||||
delete notionPageData.block
|
delete notionPageData.block
|
||||||
delete notionPageData.collection
|
delete notionPageData.collection
|
||||||
delete notionPageData.collectionQuery
|
delete notionPageData.collectionQuery
|
||||||
@@ -83,10 +80,10 @@ export async function getNotionPageData({ pageId, from }) {
|
|||||||
* @param notionPageData
|
* @param notionPageData
|
||||||
* @returns {Promise<[]|*[]>}
|
* @returns {Promise<[]|*[]>}
|
||||||
*/
|
*/
|
||||||
function getCustomNav({ allPage }) {
|
function getCustomNav({ allPages }) {
|
||||||
const customNav = []
|
const customNav = []
|
||||||
if (allPage && allPage.length > 0) {
|
if (allPages && allPages.length > 0) {
|
||||||
allPage.forEach(p => {
|
allPages.forEach(p => {
|
||||||
if (p?.status?.[0] === 'Published') {
|
if (p?.status?.[0] === 'Published') {
|
||||||
if (p?.slug?.indexOf('http') === 0) {
|
if (p?.slug?.indexOf('http') === 0) {
|
||||||
customNav.push({ icon: p.icon || null, name: p.title, to: p.slug, show: true })
|
customNav.push({ icon: p.icon || null, name: p.title, to: p.slug, show: true })
|
||||||
@@ -239,10 +236,10 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) {
|
|||||||
const tagOptions = getTagOptions(schema)
|
const tagOptions = getTagOptions(schema)
|
||||||
const categoryOptions = getCategoryOptions(schema)
|
const categoryOptions = getCategoryOptions(schema)
|
||||||
const viewIds = rawMetadata?.view_ids
|
const viewIds = rawMetadata?.view_ids
|
||||||
const data = []
|
const collectionData = []
|
||||||
const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds)
|
const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds)
|
||||||
if (pageIds?.length === 0) {
|
if (pageIds?.length === 0) {
|
||||||
console.error('获取到的文章列表为空,请检查notion模板', collectionQuery, collection, pageRecordMap)
|
console.error('获取到的文章列表为空,请检查notion模板', collectionQuery, collection, collectionView, viewIds, pageRecordMap)
|
||||||
}
|
}
|
||||||
for (let i = 0; i < pageIds.length; i++) {
|
for (let i = 0; i < pageIds.length; i++) {
|
||||||
const id = pageIds[i]
|
const id = pageIds[i]
|
||||||
@@ -251,23 +248,34 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
const properties = (await getPageProperties(id, block, schema, tagOptions)) || null
|
const properties = (await getPageProperties(id, block, schema, tagOptions)) || null
|
||||||
data.push(properties)
|
collectionData.push(properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
const allPage = data.filter(post => {
|
const allPages = collectionData.filter(post => {
|
||||||
return post.title && (post?.status?.[0] === 'Published' || post?.status?.[0] === 'Invisible') && ['Page'].indexOf(post?.type?.[0]) > -1
|
return post.title && ['Page'].indexOf(post?.type?.[0]) > -1 && (post?.status?.[0] === 'Published' || post?.status?.[0] === 'Invisible')
|
||||||
})
|
})
|
||||||
const allPosts = data.filter(post => {
|
const allPosts = collectionData.filter(post => {
|
||||||
return post.title && post?.status?.[0] === 'Published' && ['Post'].indexOf(post?.type?.[0]) > -1
|
return post.title && ['Post'].indexOf(post?.type?.[0]) > -1 && post?.status?.[0] === 'Published'
|
||||||
})
|
})
|
||||||
|
|
||||||
const customNav = getCustomNav({ allPage })
|
// Sort by date
|
||||||
|
if (BLOG.POSTS_SORT_BY === 'date') {
|
||||||
|
allPosts.sort((a, b) => {
|
||||||
|
const dateA = new Date(a?.date?.start_date || a.createdTime)
|
||||||
|
const dateB = new Date(b?.date?.start_date || b.createdTime)
|
||||||
|
return dateB - dateA
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const customNav = getCustomNav({ allPages })
|
||||||
const postCount = allPosts?.length || 0
|
const postCount = allPosts?.length || 0
|
||||||
const categories = getAllCategories({ allPosts, categoryOptions, sliceCount: BLOG.PREVIEW_CATEGORY_COUNT })
|
const categories = getAllCategories({ allPosts, categoryOptions, sliceCount: BLOG.PREVIEW_CATEGORY_COUNT })
|
||||||
const tags = getAllTags({ allPosts, tagOptions, sliceCount: BLOG.PREVIEW_TAG_COUNT })
|
const tags = getAllTags({ allPosts, tagOptions, sliceCount: BLOG.PREVIEW_TAG_COUNT })
|
||||||
const latestPosts = getLatestPosts({ allPosts, from, latestPostCount: 5 })
|
const latestPosts = getLatestPosts({ allPosts, from, latestPostCount: 5 })
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
allPages,
|
||||||
|
allPosts,
|
||||||
collection,
|
collection,
|
||||||
collectionQuery,
|
collectionQuery,
|
||||||
collectionId,
|
collectionId,
|
||||||
|
|||||||
@@ -80,12 +80,10 @@ export async function getStaticPaths() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const from = 'slug-paths'
|
const from = 'slug-paths'
|
||||||
const { allPosts } = await getGlobalNotionData({ from, pageType: ['Page'] })
|
const { allPages } = await getGlobalNotionData({ from, pageType: ['Page'] })
|
||||||
const filterPosts =
|
|
||||||
allPosts?.filter(e => e?.slug?.indexOf('http') !== 0) || []
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
paths: filterPosts.map(row => ({ params: { slug: row.slug } })),
|
paths: allPages.map(row => ({ params: { slug: row.slug } })),
|
||||||
fallback: true
|
fallback: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,19 +91,19 @@ export async function getStaticPaths() {
|
|||||||
export async function getStaticProps({ params: { slug } }) {
|
export async function getStaticProps({ params: { slug } }) {
|
||||||
const from = `slug-props-${slug}`
|
const from = `slug-props-${slug}`
|
||||||
const props = await getGlobalNotionData({ from, pageType: ['Page'] })
|
const props = await getGlobalNotionData({ from, pageType: ['Page'] })
|
||||||
const { allPosts } = props
|
const { allPages } = props
|
||||||
const post = allPosts.find(p => p.slug === slug)
|
const page = allPages?.find(p => p.slug === slug)
|
||||||
if (!post) {
|
if (!page) {
|
||||||
return { props: {}, revalidate: 1 }
|
return { props: {}, revalidate: 1 }
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
post.blockMap = await getPostBlocks(post.id, 'slug')
|
page.blockMap = await getPostBlocks(page.id, 'slug')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取文章详情失败', error)
|
console.error('获取文章详情失败', error)
|
||||||
}
|
}
|
||||||
|
|
||||||
props.post = post
|
props.post = page
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props,
|
props,
|
||||||
|
|||||||
Reference in New Issue
Block a user