-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Expand file tree
/
Copy pathnormalizeUtil.js
More file actions
105 lines (85 loc) · 2.25 KB
/
Copy pathnormalizeUtil.js
File metadata and controls
105 lines (85 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* 可能由于Notion接口升级导致数据格式变化,这里进行统一处理
* @param {*} block
* @param {*} pageId
* @returns
*/
function normalizeNotionMetadata(block, pageId) {
const rawValue = block?.[pageId]?.value
if (!rawValue) return null
return rawValue.type ? rawValue : rawValue.value ?? null
}
/**
* 兼容新老 Notion collection 结构 , 新版会用space_id 包裹一层
* 统一返回真正的 collection.value(包含 schema 的那一层)
*/
function normalizeCollection(collection) {
let current = collection
// 最多剥 5 层,兼容 { spaceId, value: { value: {...}, role } } 等新结构
for (let i = 0; i < 5; i++) {
if (!current) break
// 已经是最终形态:有 schema
if (current.schema) {
return current
}
// 常见包装:{ value: {...}, role }
if (current.value) {
current = current.value
continue
}
break
}
return current ?? {}
}
/**
* 兼容 Notion schema
* 保留原始字段 id 作为 key
*/
/**
* 兼容 Notion schema
* 保留原始字段 id 作为 key
*/
function normalizeSchema(schema = {}) {
const result = {}
Object.entries(schema).forEach(([key, value]) => {
result[key] = {
...value,
name: value?.name || '',
type: value?.type || ''
}
})
return result
}
/**
* ✅ 终极版:兼容 Notion 新老 Page Block 结构
* 最终一定返回:{ id, type, properties }
*/
function normalizePageBlock(blockItem) {
if (!blockItem) return null
let current = blockItem
for (let i = 0; i < 5; i++) {
if (!current) return null
// 针对 collection 兼容
if (
(current.type === 'collection_view_page' || current.type === 'collection_view') &&
current.collection_id
) {
return current
}
if (current.type || current.properties) {
return current
}
if (current.value) {
current = current.value
continue
}
break
}
return null
}
module.exports = {
normalizeNotionMetadata,
normalizeCollection,
normalizeSchema,
normalizePageBlock
}