Skip to content

Commit ff552b7

Browse files
committed
从Notion表格读取配置信息
1 parent 18916e4 commit ff552b7

File tree

4 files changed

+154
-9
lines changed

4 files changed

+154
-9
lines changed

lib/notion/getNotionConfig.js

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
}

lib/notion/getNotionData.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import getAllPageIds from './getAllPageIds'
88
import { getAllTags } from './getAllTags'
99
import getPageProperties from './getPageProperties'
1010
import { mapImgUrl, compressImage } from './mapImage'
11+
import { getConfigMapFromConfigPage } from './getNotionConfig'
1112

1213
/**
1314
* 获取博客数据
@@ -283,15 +284,8 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
283284
(post?.status === 'Invisible' || post?.status === 'Published')
284285
})
285286

286-
// 从notion中读取配置
287-
const configPage = collectionData.find(post => {
288-
return post && post?.type && post?.type === 'CONFIG'
289-
})
290-
291-
if (configPage) {
292-
const config = await getPostBlocks(configPage.id, 'config-table')
293-
console.log('配置中心', configPage, config)
294-
}
287+
// 站点配置优先读取配置表格,否则读取blog.config.js 文件
288+
const CONFIG = await getConfigMapFromConfigPage(collectionData) || BLOG
295289

296290
// Sort by date
297291
if (BLOG.POSTS_SORT_BY === 'date') {
@@ -311,6 +305,7 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
311305
const allNavPages = getNavPages({ allPages })
312306

313307
return {
308+
CONFIG,
314309
notice,
315310
siteInfo,
316311
allPages,

lib/notion/getPageProperties.js

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import formatDate from '../formatDate'
66
import md5 from 'js-md5'
77
import { mapImgUrl } from './mapImage'
88

9+
/**
10+
* 获取页面元素成员属性
11+
* @param {*} id
12+
* @param {*} block
13+
* @param {*} schema
14+
* @param {*} authToken
15+
* @param {*} tagOptions
16+
* @returns
17+
*/
918
export default async function getPageProperties(id, block, schema, authToken, tagOptions) {
1019
const rawProperties = Object.entries(block?.[id]?.value?.properties || [])
1120
const excludeProperties = ['date', 'select', 'multi_select', 'person']
@@ -108,6 +117,7 @@ export default async function getPageProperties(id, block, schema, authToken, ta
108117
properties.slug += '.html'
109118
}
110119
}
120+
// 密码字段md5
111121
properties.password = properties.password ? md5(properties.slug + properties.password) : ''
112122
return properties
113123
}

next-sitemap.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const BLOG = require('./blog.config')
22

3+
/**
4+
* 通常没啥用,sitemap交给 /pages/sitemap.xml.js 动态生成
5+
*/
36
module.exports = {
47
siteUrl: BLOG.LINK,
58
changefreq: 'daily',

0 commit comments

Comments
 (0)