mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 23:16:47 +00:00
51 lines
1.1 KiB
TypeScript
51 lines
1.1 KiB
TypeScript
import pMemoize from 'p-memoize'
|
|
import { getAllPagesInSpace, getCanonicalPageId } from 'notion-utils'
|
|
|
|
import * as types from './types'
|
|
import { getPage } from './notion'
|
|
|
|
export const getAllPages = pMemoize(getAllPagesImpl, { maxAge: 60000 * 5 })
|
|
|
|
export async function getAllPagesImpl(
|
|
rootNotionPageId: string,
|
|
rootNotionSpaceId: string
|
|
): Promise<Partial<types.SiteMap>> {
|
|
const pageMap = await getAllPagesInSpace(
|
|
rootNotionPageId,
|
|
rootNotionSpaceId,
|
|
getPage
|
|
// notion.getPage.bind(notion)
|
|
)
|
|
|
|
const canonicalPageMap = Object.keys(pageMap).reduce(
|
|
(map, pageId: string) => {
|
|
const recordMap = pageMap[pageId]
|
|
const canonicalPageId = getCanonicalPageId(pageId, recordMap, {
|
|
uuid: false
|
|
})
|
|
|
|
if (map[canonicalPageId]) {
|
|
console.error(
|
|
'error duplicate canonical page id',
|
|
canonicalPageId,
|
|
pageId,
|
|
map[canonicalPageId]
|
|
)
|
|
|
|
return map
|
|
} else {
|
|
return {
|
|
...map,
|
|
[canonicalPageId]: pageId
|
|
}
|
|
}
|
|
},
|
|
{}
|
|
)
|
|
|
|
return {
|
|
pageMap,
|
|
canonicalPageMap
|
|
}
|
|
}
|