mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-07 07:26:46 +00:00
文章路由支持配置,完善自定义Notion字段配置
This commit is contained in:
@@ -21,8 +21,7 @@ import getPageProperties from './getPageProperties'
|
||||
*/
|
||||
export async function getGlobalNotionData({
|
||||
pageId = BLOG.NOTION_PAGE_ID,
|
||||
from,
|
||||
pageType = ['Post']
|
||||
from
|
||||
}) {
|
||||
// 获取Notion数据
|
||||
const notionPageData = deepClone(await getNotionPageData({ pageId, from }))
|
||||
@@ -42,7 +41,9 @@ export async function getGlobalNotionData({
|
||||
* @param {*}} param0
|
||||
* @returns
|
||||
*/
|
||||
function getLatestPosts({ allPosts, from, latestPostCount }) {
|
||||
function getLatestPosts({ allPages, from, latestPostCount }) {
|
||||
const allPosts = allPages.filter(page => page.type === 'Post')
|
||||
|
||||
const latestPosts = Object.create(allPosts).sort((a, b) => {
|
||||
const dateA = new Date(a?.lastEditedTime || a?.createdTime || a?.date?.start_date)
|
||||
const dateB = new Date(b?.lastEditedTime || b?.createdTime || b?.date?.start_date)
|
||||
@@ -82,7 +83,7 @@ function getCustomNav({ allPages }) {
|
||||
const customNav = []
|
||||
if (allPages && allPages.length > 0) {
|
||||
allPages.forEach(p => {
|
||||
if (p?.status?.[0] === 'Published') {
|
||||
if (p?.status === 'Published' && p?.type === 'Page') {
|
||||
if (p?.slug?.indexOf('http') === 0) {
|
||||
customNav.push({ icon: p.icon || null, name: p.title, to: p.slug, show: true })
|
||||
} else {
|
||||
@@ -101,7 +102,7 @@ function getCustomNav({ allPages }) {
|
||||
*/
|
||||
function getTagOptions(schema) {
|
||||
if (!schema) return {}
|
||||
const tagSchema = Object.values(schema).find(e => e.name === 'tags')
|
||||
const tagSchema = Object.values(schema).find(e => e.name === BLOG.NOTION_PROPERTY_NAME.tags)
|
||||
return tagSchema?.options || []
|
||||
}
|
||||
|
||||
@@ -112,7 +113,7 @@ function getTagOptions(schema) {
|
||||
*/
|
||||
function getCategoryOptions(schema) {
|
||||
if (!schema) return {}
|
||||
const categorySchema = Object.values(schema).find(e => e.name === 'category')
|
||||
const categorySchema = Object.values(schema).find(e => e.name === BLOG.NOTION_PROPERTY_NAME.category)
|
||||
return categorySchema?.options || []
|
||||
}
|
||||
|
||||
@@ -121,7 +122,8 @@ function getCategoryOptions(schema) {
|
||||
* @param allPosts
|
||||
* @returns {Promise<{}|*[]>}
|
||||
*/
|
||||
function getAllCategories({ allPosts, categoryOptions, sliceCount = 0 }) {
|
||||
function getAllCategories({ allPages, categoryOptions, sliceCount = 0 }) {
|
||||
const allPosts = allPages.filter(page => page.type === 'Post')
|
||||
if (!allPosts || !categoryOptions) {
|
||||
return []
|
||||
}
|
||||
@@ -220,7 +222,7 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) {
|
||||
// Check Type Page-Database和Inline-Database
|
||||
if (
|
||||
rawMetadata?.type !== 'collection_view_page' &&
|
||||
rawMetadata?.type !== 'collection_view'
|
||||
rawMetadata?.type !== 'collection_view'
|
||||
) {
|
||||
console.warn(`pageId "${pageId}" is not a database`)
|
||||
return null
|
||||
@@ -252,24 +254,23 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) {
|
||||
}
|
||||
}
|
||||
// 读取映射 配置
|
||||
console.log('当前Notion映射配置-(在blog.config.js中配置)', BLOG.NOTION_PROPERTY_NAME)
|
||||
const { type, status } = BLOG.NOTION_PROPERTY_NAME
|
||||
let postCount = 0
|
||||
|
||||
const allPages = collectionData.filter(post => {
|
||||
return post && post[type] &&
|
||||
['Page'].indexOf(post[type]?.[0]) > -1 &&
|
||||
(post[status]?.[0] === 'Published' || post[status]?.[0] === 'Invisible')
|
||||
})
|
||||
const allPosts = collectionData.filter(post => {
|
||||
return post && post[status] &&
|
||||
['Post'].indexOf(post[type]?.[0]) > -1 &&
|
||||
post[status]?.[0] === 'Published'
|
||||
})
|
||||
console.log('全部单页', allPages.length, '全部博客', allPosts.length)
|
||||
if (post.type === 'Post' && (post.status === 'Published' || post.status === 'Invisible')) {
|
||||
postCount++
|
||||
}
|
||||
|
||||
return post &&
|
||||
post.type &&
|
||||
(post.type === 'Post' || post.type === 'Page') &&
|
||||
(post.status === 'Published' || post.status === 'Invisible')
|
||||
}
|
||||
)
|
||||
|
||||
// Sort by date
|
||||
if (BLOG.POSTS_SORT_BY === 'date') {
|
||||
allPosts.sort((a, b) => {
|
||||
allPages.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
|
||||
@@ -277,15 +278,13 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) {
|
||||
}
|
||||
|
||||
const customNav = getCustomNav({ allPages })
|
||||
const postCount = allPosts?.length || 0
|
||||
const categories = getAllCategories({ allPosts, categoryOptions, sliceCount: BLOG.PREVIEW_CATEGORY_COUNT })
|
||||
const tags = getAllTags({ allPosts, tagOptions, sliceCount: BLOG.PREVIEW_TAG_COUNT })
|
||||
const latestPosts = getLatestPosts({ allPosts, from, latestPostCount: 5 })
|
||||
const categories = getAllCategories({ allPages, categoryOptions, sliceCount: BLOG.PREVIEW_CATEGORY_COUNT })
|
||||
const tags = getAllTags({ allPages, tagOptions, sliceCount: BLOG.PREVIEW_TAG_COUNT })
|
||||
const latestPosts = getLatestPosts({ allPages, from, latestPostCount: 5 })
|
||||
|
||||
return {
|
||||
siteInfo,
|
||||
allPages,
|
||||
allPosts,
|
||||
collection,
|
||||
collectionQuery,
|
||||
collectionId,
|
||||
|
||||
Reference in New Issue
Block a user