mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 15:09:47 +00:00
81 lines
2.3 KiB
TypeScript
81 lines
2.3 KiB
TypeScript
import pMap from 'p-map'
|
|
import pMemoize from 'p-memoize'
|
|
import { ExtendedRecordMap, SearchParams, SearchResults } from 'notion-types'
|
|
import { mergeRecordMaps } from 'notion-utils'
|
|
|
|
import { notion } from './notion-api'
|
|
import { getPreviewImageMap } from './preview-images'
|
|
import { getTweetAstMap } from './tweet-embeds'
|
|
import {
|
|
isPreviewImageSupportEnabled,
|
|
isTweetEmbedSupportEnabled,
|
|
navigationStyle,
|
|
navigationLinks
|
|
} from './config'
|
|
|
|
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()
|
|
|
|
const preLength = Object.keys(recordMap.block).length
|
|
if (navigationLinkRecordMaps?.length) {
|
|
recordMap = navigationLinkRecordMaps.reduce(
|
|
(map, navigationLinkRecordMap) =>
|
|
mergeRecordMaps(map, navigationLinkRecordMap),
|
|
recordMap
|
|
)
|
|
|
|
const postLength = Object.keys(recordMap.block).length
|
|
|
|
console.log('BLOCKS', { preLength, postLength })
|
|
}
|
|
}
|
|
|
|
if (isPreviewImageSupportEnabled) {
|
|
const previewImageMap = await getPreviewImageMap(recordMap)
|
|
;(recordMap as any).preview_images = previewImageMap
|
|
}
|
|
|
|
if (isTweetEmbedSupportEnabled) {
|
|
const tweetAstMap = await getTweetAstMap(recordMap)
|
|
;(recordMap as any).tweetAstMap = tweetAstMap
|
|
}
|
|
|
|
return recordMap
|
|
}
|
|
|
|
export async function search(params: SearchParams): Promise<SearchResults> {
|
|
return notion.search(params)
|
|
}
|