import { type ExtendedRecordMap, type SearchParams, type 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 { getTweetsMap } from './get-tweets' import { notion } from './notion-api' import { getPreviewImageMap } from './preview-images' const getNavigationLinkPages = pMemoize( async (): Promise => { 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 { 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 } await getTweetsMap(recordMap) return recordMap } export async function search(params: SearchParams): Promise { return notion.search(params) }