支持INLINE_CONFIG

This commit is contained in:
tangly1024.com
2024-04-11 17:57:56 +08:00
parent b7d6485138
commit 69ff9ecb60
2 changed files with 67 additions and 13 deletions

View File

@@ -29,7 +29,7 @@ export async function getGlobalData({
locale
}) {
// 获取站点数据 如果pageId有逗号隔开则分次取数据
const siteIds = pageId.split(',')
const siteIds = pageId?.split(',') || []
let data = EmptyData(pageId)
try {

View File

@@ -7,8 +7,8 @@
*
*/
import { getDateValue, getTextContent } from 'notion-utils'
import { getPostBlocks } from './getPostBlocks'
import getAllPageIds from './getAllPageIds'
import { getPostBlocks } from './getPostBlocks'
/**
* 从Notion中读取Config配置表
@@ -23,8 +23,15 @@ export async function getConfigMapFromConfigPage(allPages) {
console.warn('[Notion配置] 忽略的配置')
return null
}
// 找到Config类
const configPage = allPages?.find(post => {
return post && post?.type && (post?.type === 'CONFIG' || post?.type === 'config' || post?.type === 'Config')
return (
post &&
post?.type &&
(post?.type === 'CONFIG' ||
post?.type === 'config' ||
post?.type === 'Config')
)
})
if (!configPage) {
@@ -43,21 +50,26 @@ export async function getConfigMapFromConfigPage(allPages) {
}
if (!content) {
console.warn('[Notion配置] 未找到配置表格', pageRecordMap.block[configPageId], pageRecordMap.block[configPageId].value)
console.warn(
'[Notion配置] 未找到配置表格',
pageRecordMap.block[configPageId],
pageRecordMap.block[configPageId].value
)
return null
}
// 找到配置文件中的database
// for (const contentId of content) {
// console.log('内容', contentId, configPageRecordMap.block[contentId].value.type === 'collection_view')
// }
// 找到PAGE文件中的database
const configTableId = content?.find(contentId => {
return pageRecordMap.block[contentId].value.type === 'collection_view'
})
// eslint-disable-next-line no-constant-condition, no-self-compare
if (!configTableId) {
console.warn('[Notion配置]未找到配置表格数据', pageRecordMap.block[configPageId], pageRecordMap.block[configPageId].value)
console.warn(
'[Notion配置]未找到配置表格数据',
pageRecordMap.block[configPageId],
pageRecordMap.block[configPageId].value
)
return null
}
@@ -67,7 +79,8 @@ export async function getConfigMapFromConfigPage(allPages) {
const rawMetadata = databaseRecordMap.value
// Check Type Page-Database和Inline-Database
if (
rawMetadata?.type !== 'collection_view_page' && rawMetadata?.type !== 'collection_view'
rawMetadata?.type !== 'collection_view_page' &&
rawMetadata?.type !== 'collection_view'
) {
console.error(`pageId "${configTableId}" is not a database`)
return null
@@ -79,9 +92,21 @@ export async function getConfigMapFromConfigPage(allPages) {
const collectionView = pageRecordMap.collection_view
const schema = collection?.schema
const viewIds = rawMetadata?.view_ids
const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds)
const pageIds = getAllPageIds(
collectionQuery,
collectionId,
collectionView,
viewIds
)
if (pageIds?.length === 0) {
console.error('[Notion配置]获取到的文章列表为空请检查notion模板', collectionQuery, collection, collectionView, viewIds, databaseRecordMap)
console.error(
'[Notion配置]获取到的文章列表为空请检查notion模板',
collectionQuery,
collection,
collectionView,
viewIds,
databaseRecordMap
)
}
// 遍历用户的表格
for (let i = 0; i < pageIds.length; i++) {
@@ -136,5 +161,34 @@ export async function getConfigMapFromConfigPage(allPages) {
}
}
return notionConfig
// 最后检查Notion_Config页面的INLINE_CONFIG是否是一个js对象
const inlineConfigs = parseConfig(configPage?.INLINE_CONFIG)
return Object.assign({}, notionConfig, inlineConfigs)
}
/**
* 解析INLINE_CONFIG
* @param {*} configString
* @returns
*/
export function parseConfig(configString) {
if (!configString) {
return {}
}
try {
// 尝试解析为 JSON 对象
const jsonConfig = JSON.parse(configString)
return jsonConfig
} catch (error) {
// 如果解析失败,则尝试使用 eval()
try {
// eslint-disable-next-line no-eval
const config = eval('(' + configString + ')')
return config
} catch (evalError) {
console.error('解析 INLINE_CONFIG 配置时出错:', evalError)
return {}
}
}
}