-
-
Notifications
You must be signed in to change notification settings - Fork 612
/
Copy pathget-page-property-from-id.ts
89 lines (76 loc) · 2.86 KB
/
get-page-property-from-id.ts
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
import { Block, DateFormat, ExtendedRecordMap } from 'notion-types'
import { getTextContent } from './get-text-content'
/**
* Gets the value of a collection property for a given page (collection item).
*
* @param propertyId property id
* @param block Page block, often be first block in blockMap
* @param recordMap
* @returns - The return value types will follow the following principles:
* 1. if property is date type, it will return `number` or `number[]`(depends on `End Date` switch)
* 2. property is text-like will return `string`
* 3. multi select property will return `string[]`
* 4. checkbox property return `boolean`
* @todo complete all no-text property type
*/
export function getPagePropertyFromId<
T = string | number | boolean | string[] | number[]
>(propertyId: string, block: Block, recordMap: ExtendedRecordMap): T
export function getPagePropertyFromId(
propertyId: string,
block: Block,
recordMap: ExtendedRecordMap
) {
try {
if (!block.properties || !Object.keys(recordMap.collection)) {
// console.warn(
// `block ${block.id} has no properties or this recordMap has no collection record`
// )
return null
}
const collection = recordMap.collection[block.parent_id]?.value
if (collection) {
if (!propertyId) {
return null
}
const { type } = collection.schema[propertyId]
const content = getTextContent(block.properties[propertyId])
switch (type) {
case 'created_time':
return block.created_time
case 'multi_select':
return content.split(',')
case 'date': {
const property = block.properties[propertyId] as [['‣', [DateFormat]]]
const formatDate = property[0][1][0][1]
if (formatDate.type == 'datetime') {
return new Date(
`${formatDate.start_date} ${formatDate.start_time}`
).getTime()
} else if (formatDate.type == 'date') {
return new Date(formatDate.start_date).getTime()
} else if (formatDate.type == 'datetimerange') {
const { start_date, start_time, end_date, end_time } = formatDate
const startTime = new Date(`${start_date} ${start_time}`).getTime()
const endTime = new Date(`${end_date} ${end_time}`).getTime()
return [startTime, endTime]
} else {
const startTime = new Date(formatDate.start_date).getTime()
const endTime = new Date(formatDate.end_date).getTime()
return [startTime, endTime]
}
}
case 'checkbox':
return content == 'Yes'
case 'last_edited_time':
return block.last_edited_time
default:
return content
}
}
} catch {
// ensure that no matter what, we don't throw errors because of an unexpected
// collection data format
}
return null
}