文章路由支持配置,完善自定义Notion字段配置

This commit is contained in:
tangly
2022-10-18 16:30:38 +08:00
parent c5efc841ba
commit 1fcafeb214
32 changed files with 91 additions and 187 deletions

View File

@@ -7,7 +7,9 @@ import { isIterable } from '../utils'
* @param tagOptions tags的下拉选项
* @returns {Promise<{}|*[]>}
*/
export function getAllTags({ allPosts, sliceCount = 0, tagOptions }) {
export function getAllTags({ allPages, sliceCount = 0, tagOptions }) {
const allPosts = allPages.filter(page => page.type === 'Post')
if (!allPosts || !tagOptions) {
return []
}

View File

@@ -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,

View File

@@ -58,6 +58,7 @@ async function getPageProperties(id, block, schema, authToken, tagOptions, siteI
}
}
}
// 设置自定义字段
const fieldNames = BLOG.NOTION_PROPERTY_NAME
if (fieldNames) {
@@ -65,7 +66,16 @@ async function getPageProperties(id, block, schema, authToken, tagOptions, siteI
if (fieldNames[key] && properties[fieldNames[key]]) properties[key] = properties[fieldNames[key]]
})
}
properties.slug = properties.slug ?? properties.id
properties.type = properties.type[0]
properties.status = properties.status[0]
if (properties.type === 'Post') {
properties.slug = BLOG.POST_URL_PREFIX + '/' + (properties.slug ?? properties.id)
} else {
properties.slug = (properties.slug ?? properties.id)
}
properties.createdTime = formatDate(new Date(value.created_time).toString(), BLOG.LANG)
properties.lastEditedTime = formatDate(new Date(value?.last_edited_time).toString(), BLOG.LANG)
properties.fullWidth = value.format?.page_full_width ?? false

View File

@@ -38,7 +38,7 @@ export async function generateRss(posts) {
feed.addItem({
title: post.title,
guid: `${post.id}`,
link: `${BLOG.LINK}/article/${post.slug}`,
link: `${BLOG.LINK}/${post.slug}`,
description: post.summary,
content: await createFeedContent(post),
date: new Date(post?.date?.start_date || post?.createdTime)