mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-02 15:10:16 +00:00
支持INLINE_CONFIG
This commit is contained in:
@@ -29,7 +29,7 @@ export async function getGlobalData({
|
|||||||
locale
|
locale
|
||||||
}) {
|
}) {
|
||||||
// 获取站点数据 , 如果pageId有逗号隔开则分次取数据
|
// 获取站点数据 , 如果pageId有逗号隔开则分次取数据
|
||||||
const siteIds = pageId.split(',')
|
const siteIds = pageId?.split(',') || []
|
||||||
let data = EmptyData(pageId)
|
let data = EmptyData(pageId)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -7,8 +7,8 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
import { getDateValue, getTextContent } from 'notion-utils'
|
import { getDateValue, getTextContent } from 'notion-utils'
|
||||||
import { getPostBlocks } from './getPostBlocks'
|
|
||||||
import getAllPageIds from './getAllPageIds'
|
import getAllPageIds from './getAllPageIds'
|
||||||
|
import { getPostBlocks } from './getPostBlocks'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从Notion中读取Config配置表
|
* 从Notion中读取Config配置表
|
||||||
@@ -23,8 +23,15 @@ export async function getConfigMapFromConfigPage(allPages) {
|
|||||||
console.warn('[Notion配置] 忽略的配置')
|
console.warn('[Notion配置] 忽略的配置')
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
// 找到Config类
|
||||||
const configPage = allPages?.find(post => {
|
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) {
|
if (!configPage) {
|
||||||
@@ -43,21 +50,26 @@ export async function getConfigMapFromConfigPage(allPages) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!content) {
|
if (!content) {
|
||||||
console.warn('[Notion配置] 未找到配置表格', pageRecordMap.block[configPageId], pageRecordMap.block[configPageId].value)
|
console.warn(
|
||||||
|
'[Notion配置] 未找到配置表格',
|
||||||
|
pageRecordMap.block[configPageId],
|
||||||
|
pageRecordMap.block[configPageId].value
|
||||||
|
)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
// 找到配置文件中的database
|
// 找到PAGE文件中的database
|
||||||
// for (const contentId of content) {
|
|
||||||
// console.log('内容', contentId, configPageRecordMap.block[contentId].value.type === 'collection_view')
|
|
||||||
// }
|
|
||||||
const configTableId = content?.find(contentId => {
|
const configTableId = content?.find(contentId => {
|
||||||
return pageRecordMap.block[contentId].value.type === 'collection_view'
|
return pageRecordMap.block[contentId].value.type === 'collection_view'
|
||||||
})
|
})
|
||||||
|
|
||||||
// eslint-disable-next-line no-constant-condition, no-self-compare
|
// eslint-disable-next-line no-constant-condition, no-self-compare
|
||||||
if (!configTableId) {
|
if (!configTableId) {
|
||||||
console.warn('[Notion配置]未找到配置表格数据', pageRecordMap.block[configPageId], pageRecordMap.block[configPageId].value)
|
console.warn(
|
||||||
|
'[Notion配置]未找到配置表格数据',
|
||||||
|
pageRecordMap.block[configPageId],
|
||||||
|
pageRecordMap.block[configPageId].value
|
||||||
|
)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +79,8 @@ export async function getConfigMapFromConfigPage(allPages) {
|
|||||||
const rawMetadata = databaseRecordMap.value
|
const rawMetadata = databaseRecordMap.value
|
||||||
// Check Type Page-Database和Inline-Database
|
// Check Type Page-Database和Inline-Database
|
||||||
if (
|
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`)
|
console.error(`pageId "${configTableId}" is not a database`)
|
||||||
return null
|
return null
|
||||||
@@ -79,9 +92,21 @@ export async function getConfigMapFromConfigPage(allPages) {
|
|||||||
const collectionView = pageRecordMap.collection_view
|
const collectionView = pageRecordMap.collection_view
|
||||||
const schema = collection?.schema
|
const schema = collection?.schema
|
||||||
const viewIds = rawMetadata?.view_ids
|
const viewIds = rawMetadata?.view_ids
|
||||||
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配置]获取到的文章列表为空,请检查notion模板', collectionQuery, collection, collectionView, viewIds, databaseRecordMap)
|
console.error(
|
||||||
|
'[Notion配置]获取到的文章列表为空,请检查notion模板',
|
||||||
|
collectionQuery,
|
||||||
|
collection,
|
||||||
|
collectionView,
|
||||||
|
viewIds,
|
||||||
|
databaseRecordMap
|
||||||
|
)
|
||||||
}
|
}
|
||||||
// 遍历用户的表格
|
// 遍历用户的表格
|
||||||
for (let i = 0; i < pageIds.length; i++) {
|
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 {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user