mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 15:09:47 +00:00
60 lines
1.3 KiB
TypeScript
60 lines
1.3 KiB
TypeScript
import pMemoize from 'p-memoize'
|
|
import { getAllPagesInSpace } from 'notion-utils'
|
|
|
|
import * as types from './types'
|
|
import { includeNotionIdInUrls } from './config'
|
|
import { notion } from './notion'
|
|
import { getCanonicalPageId } from './get-canonical-page-id'
|
|
|
|
const uuid = !!includeNotionIdInUrls
|
|
|
|
export const getAllPages = pMemoize(getAllPagesImpl, {
|
|
cacheKey: (...args) => JSON.stringify(args)
|
|
})
|
|
|
|
export async function getAllPagesImpl(
|
|
rootNotionPageId: string,
|
|
rootNotionSpaceId: string
|
|
): Promise<Partial<types.SiteMap>> {
|
|
const pageMap = await getAllPagesInSpace(
|
|
rootNotionPageId,
|
|
rootNotionSpaceId,
|
|
notion.getPage.bind(notion)
|
|
)
|
|
|
|
const canonicalPageMap = Object.keys(pageMap).reduce(
|
|
(map, pageId: string) => {
|
|
const recordMap = pageMap[pageId]
|
|
if (!recordMap) {
|
|
throw new Error(`Error loading page "${pageId}"`)
|
|
}
|
|
|
|
const canonicalPageId = getCanonicalPageId(pageId, recordMap, {
|
|
uuid
|
|
})
|
|
|
|
if (map[canonicalPageId]) {
|
|
console.error(
|
|
'error duplicate canonical page id',
|
|
canonicalPageId,
|
|
pageId,
|
|
map[canonicalPageId]
|
|
)
|
|
|
|
return map
|
|
} else {
|
|
return {
|
|
...map,
|
|
[canonicalPageId]: pageId
|
|
}
|
|
}
|
|
},
|
|
{}
|
|
)
|
|
|
|
return {
|
|
pageMap,
|
|
canonicalPageMap
|
|
}
|
|
}
|