|
| 1 | +/** |
| 2 | + * 从Notion中读取站点配置; |
| 3 | + * 在Notion模板中创建一个类型为CONFIG的页面,再添加一个数据库表格,即可用于填写配置 |
| 4 | + * Notion数据库配置优先级最高,将覆盖vercel环境变量以及blog.config.js中的配置 |
| 5 | + * --注意-- |
| 6 | + * 数据库请从模板复制 https://www.notion.so/tanghh/287869a92e3d4d598cf366bd6994755e |
| 7 | + * |
| 8 | + */ |
| 9 | +import { getDateValue, getTextContent } from 'notion-utils' |
| 10 | +import { getPostBlocks } from './getPostBlocks' |
| 11 | +import getAllPageIds from './getAllPageIds' |
| 12 | +import BLOG from '@/blog.config' |
| 13 | + |
| 14 | +/** |
| 15 | + * 从Notion中读取Config配置表 |
| 16 | + * @param {*} allPages |
| 17 | + * @returns |
| 18 | + */ |
| 19 | +export async function getConfigMapFromConfigPage(allPages) { |
| 20 | + // 默认返回配置文件 |
| 21 | + const notionConfig = BLOG |
| 22 | + |
| 23 | + if (!allPages || !Array.isArray(allPages) || allPages.length === 0) { |
| 24 | + console.warn('[Notion配置] 忽略的配置', allPages, typeof allPages) |
| 25 | + return null |
| 26 | + } |
| 27 | + const configPage = allPages?.find(post => { |
| 28 | + return post && post?.type && post?.type === 'CONFIG' |
| 29 | + }) |
| 30 | + |
| 31 | + if (!configPage) { |
| 32 | + console.warn('[Notion配置]未找到配置页面', allPages, typeof allPages) |
| 33 | + return null |
| 34 | + } |
| 35 | + const configPageId = configPage.id |
| 36 | + |
| 37 | + console.log('[Notion配置]请求配置数据 ', configPage.id) |
| 38 | + const pageRecordMap = await getPostBlocks(configPageId, 'config-table') |
| 39 | + // console.log('配置中心Page', configPageId, pageRecordMap) |
| 40 | + const content = pageRecordMap.block[configPageId].value.content |
| 41 | + |
| 42 | + if (!content) { |
| 43 | + console.warn('[Notion配置]未找到配置表格', pageRecordMap.block[configPageId], pageRecordMap.block[configPageId].value) |
| 44 | + return null |
| 45 | + } |
| 46 | + |
| 47 | + // 找到配置文件中的database |
| 48 | + // for (const contentId of content) { |
| 49 | + // console.log('内容', contentId, configPageRecordMap.block[contentId].value.type === 'collection_view') |
| 50 | + // } |
| 51 | + const configTableId = content?.find(contentId => { |
| 52 | + return pageRecordMap.block[contentId].value.type === 'collection_view' |
| 53 | + }) |
| 54 | + |
| 55 | + // eslint-disable-next-line no-constant-condition, no-self-compare |
| 56 | + if (!configTableId) { |
| 57 | + console.warn('[Notion配置]未找到配置表格数据', pageRecordMap.block[configPageId], pageRecordMap.block[configPageId].value) |
| 58 | + return null |
| 59 | + } |
| 60 | + |
| 61 | + // 页面查找 |
| 62 | + const databaseRecordMap = pageRecordMap.block[configTableId] |
| 63 | + const block = pageRecordMap.block || {} |
| 64 | + const rawMetadata = databaseRecordMap.value |
| 65 | + // Check Type Page-Database和Inline-Database |
| 66 | + if ( |
| 67 | + rawMetadata?.type !== 'collection_view_page' && rawMetadata?.type !== 'collection_view' |
| 68 | + ) { |
| 69 | + console.error(`pageId "${configTableId}" is not a database`) |
| 70 | + return null |
| 71 | + } |
| 72 | + console.log('表格', databaseRecordMap, block, rawMetadata) |
| 73 | + const collectionId = rawMetadata?.collection_id |
| 74 | + const collection = pageRecordMap.collection[collectionId].value |
| 75 | + const collectionQuery = pageRecordMap.collection_query |
| 76 | + const collectionView = pageRecordMap.collection_view |
| 77 | + const schema = collection?.schema |
| 78 | + const viewIds = rawMetadata?.view_ids |
| 79 | + const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds) |
| 80 | + if (pageIds?.length === 0) { |
| 81 | + console.error('[Notion配置]获取到的文章列表为空,请检查notion模板', collectionQuery, collection, collectionView, viewIds, databaseRecordMap) |
| 82 | + } |
| 83 | + // 遍历用户的表格 |
| 84 | + for (let i = 0; i < pageIds.length; i++) { |
| 85 | + const id = pageIds[i] |
| 86 | + const value = block[id]?.value |
| 87 | + if (!value) { |
| 88 | + continue |
| 89 | + } |
| 90 | + const rawProperties = Object.entries(block?.[id]?.value?.properties || []) |
| 91 | + const excludeProperties = ['date', 'select', 'multi_select', 'person'] |
| 92 | + const properties = {} |
| 93 | + for (let i = 0; i < rawProperties.length; i++) { |
| 94 | + const [key, val] = rawProperties[i] |
| 95 | + properties.id = id |
| 96 | + if (schema[key]?.type && !excludeProperties.includes(schema[key].type)) { |
| 97 | + properties[schema[key].name] = getTextContent(val) |
| 98 | + } else { |
| 99 | + switch (schema[key]?.type) { |
| 100 | + case 'date': { |
| 101 | + const dateProperty = getDateValue(val) |
| 102 | + delete dateProperty.type |
| 103 | + properties[schema[key].name] = dateProperty |
| 104 | + break |
| 105 | + } |
| 106 | + case 'select': |
| 107 | + case 'multi_select': { |
| 108 | + const selects = getTextContent(val) |
| 109 | + if (selects[0]?.length) { |
| 110 | + properties[schema[key].name] = selects.split(',') |
| 111 | + } |
| 112 | + break |
| 113 | + } |
| 114 | + default: |
| 115 | + break |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (properties) { |
| 121 | + // 将表格中的字段映射成 英文 |
| 122 | + const config = { |
| 123 | + enable: properties['启用'] === 'Yes', |
| 124 | + key: properties['配置名'], |
| 125 | + value: properties['配置值'] |
| 126 | + } |
| 127 | + |
| 128 | + // 只导入生效的配置 |
| 129 | + if (config.enable) { |
| 130 | + console.log('[覆盖代码配置]', config.key) |
| 131 | + notionConfig[config.key] = config.value |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return notionConfig |
| 137 | +} |
0 commit comments