|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { getDateValue, getTextContent } from 'notion-utils' |
|
import { getPostBlocks } from './getPostBlocks' |
|
import getAllPageIds from './getAllPageIds' |
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function getConfigMapFromConfigPage(allPages) { |
|
|
|
const notionConfig = {} |
|
|
|
if (!allPages || !Array.isArray(allPages) || allPages.length === 0) { |
|
console.warn('[Notion配置] 忽略的配置') |
|
return null |
|
} |
|
const configPage = allPages?.find(post => { |
|
return post && post?.type && (post?.type === 'CONFIG' || post?.type === 'config' || post?.type === 'Config') |
|
}) |
|
|
|
if (!configPage) { |
|
console.warn('[Notion配置] 未找到配置页面') |
|
return null |
|
} |
|
const configPageId = configPage.id |
|
|
|
let pageRecordMap = await getPostBlocks(configPageId, 'config-table') |
|
|
|
let content = pageRecordMap.block[configPageId].value.content |
|
for (const table of ['Config-Table', 'CONFIG-TABLE']) { |
|
if (content) break |
|
pageRecordMap = await getPostBlocks(configPageId, table) |
|
content = pageRecordMap.block[configPageId].value.content |
|
} |
|
|
|
if (!content) { |
|
console.warn('[Notion配置] 未找到配置表格', pageRecordMap.block[configPageId], pageRecordMap.block[configPageId].value) |
|
return null |
|
} |
|
|
|
|
|
|
|
|
|
|
|
const configTableId = content?.find(contentId => { |
|
return pageRecordMap.block[contentId].value.type === 'collection_view' |
|
}) |
|
|
|
|
|
if (!configTableId) { |
|
console.warn('[Notion配置]未找到配置表格数据', pageRecordMap.block[configPageId], pageRecordMap.block[configPageId].value) |
|
return null |
|
} |
|
|
|
|
|
const databaseRecordMap = pageRecordMap.block[configTableId] |
|
const block = pageRecordMap.block || {} |
|
const rawMetadata = databaseRecordMap.value |
|
|
|
if ( |
|
rawMetadata?.type !== 'collection_view_page' && rawMetadata?.type !== 'collection_view' |
|
) { |
|
console.error(`pageId "${configTableId}" is not a database`) |
|
return null |
|
} |
|
|
|
const collectionId = rawMetadata?.collection_id |
|
const collection = pageRecordMap.collection[collectionId].value |
|
const collectionQuery = pageRecordMap.collection_query |
|
const collectionView = pageRecordMap.collection_view |
|
const schema = collection?.schema |
|
const viewIds = rawMetadata?.view_ids |
|
const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds) |
|
if (pageIds?.length === 0) { |
|
console.error('[Notion配置]获取到的文章列表为空,请检查notion模板', collectionQuery, collection, collectionView, viewIds, databaseRecordMap) |
|
} |
|
|
|
for (let i = 0; i < pageIds.length; i++) { |
|
const id = pageIds[i] |
|
const value = block[id]?.value |
|
if (!value) { |
|
continue |
|
} |
|
const rawProperties = Object.entries(block?.[id]?.value?.properties || []) |
|
const excludeProperties = ['date', 'select', 'multi_select', 'person'] |
|
const properties = {} |
|
for (let i = 0; i < rawProperties.length; i++) { |
|
const [key, val] = rawProperties[i] |
|
properties.id = id |
|
if (schema[key]?.type && !excludeProperties.includes(schema[key].type)) { |
|
properties[schema[key].name] = getTextContent(val) |
|
} else { |
|
switch (schema[key]?.type) { |
|
case 'date': { |
|
const dateProperty = getDateValue(val) |
|
delete dateProperty.type |
|
properties[schema[key].name] = dateProperty |
|
break |
|
} |
|
case 'select': |
|
case 'multi_select': { |
|
const selects = getTextContent(val) |
|
if (selects[0]?.length) { |
|
properties[schema[key].name] = selects.split(',') |
|
} |
|
break |
|
} |
|
default: |
|
break |
|
} |
|
} |
|
} |
|
|
|
if (properties) { |
|
|
|
const config = { |
|
enable: (properties['启用'] || properties.Enable) === 'Yes', |
|
key: properties['配置名'] || properties.Name, |
|
value: properties['配置值'] || properties.Value |
|
} |
|
|
|
|
|
if (config.enable) { |
|
|
|
notionConfig[config.key] = config.value |
|
} |
|
} |
|
} |
|
|
|
return notionConfig |
|
} |
|
|