forked from transitive-bullshit/nextjs-notion-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnotion.ts
68 lines (58 loc) · 1.91 KB
/
notion.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
import { ExtendedRecordMap, SearchParams, SearchResults } from 'notion-types'
import { mergeRecordMaps } from 'notion-utils'
import pMap from 'p-map'
import pMemoize from 'p-memoize'
import {
isPreviewImageSupportEnabled,
navigationLinks,
navigationStyle
} from './config'
import { notion } from './notion-api'
import { getPreviewImageMap } from './preview-images'
const getNavigationLinkPages = pMemoize(
async (): Promise<ExtendedRecordMap[]> => {
const navigationLinkPageIds = (navigationLinks || [])
.map((link) => link.pageId)
.filter(Boolean)
if (navigationStyle !== 'default' && navigationLinkPageIds.length) {
return pMap(
navigationLinkPageIds,
async (navigationLinkPageId) =>
notion.getPage(navigationLinkPageId, {
chunkLimit: 1,
fetchMissingBlocks: false,
fetchCollections: false,
signFileUrls: false
}),
{
concurrency: 4
}
)
}
return []
}
)
export async function getPage(pageId: string): Promise<ExtendedRecordMap> {
let recordMap = await notion.getPage(pageId)
if (navigationStyle !== 'default') {
// ensure that any pages linked to in the custom navigation header have
// their block info fully resolved in the page record map so we know
// the page title, slug, etc.
const navigationLinkRecordMaps = await getNavigationLinkPages()
if (navigationLinkRecordMaps?.length) {
recordMap = navigationLinkRecordMaps.reduce(
(map, navigationLinkRecordMap) =>
mergeRecordMaps(map, navigationLinkRecordMap),
recordMap
)
}
}
if (isPreviewImageSupportEnabled) {
const previewImageMap = await getPreviewImageMap(recordMap)
;(recordMap as any).preview_images = previewImageMap
}
return recordMap
}
export async function search(params: SearchParams): Promise<SearchResults> {
return notion.search(params)
}