mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 15:09:47 +00:00
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import * as types from './types'
|
|
import { isDev } from './config'
|
|
import { getCanonicalPageId, uuidToId, parsePageId } from 'notion-utils'
|
|
|
|
// include UUIDs in page URLs during local development but not in production
|
|
// (they're nice for debugging and speed up local dev)
|
|
const uuid = !!isDev
|
|
|
|
export const mapPageUrl = (
|
|
site: types.Site,
|
|
recordMap: types.ExtendedRecordMap,
|
|
searchParams: URLSearchParams
|
|
) => (pageId = '') => {
|
|
if (uuidToId(pageId) === site.rootNotionPageId) {
|
|
return createUrl('/', searchParams)
|
|
} else {
|
|
return createUrl(
|
|
`/${getCanonicalPageId(pageId, recordMap, { uuid })}`,
|
|
searchParams
|
|
)
|
|
}
|
|
}
|
|
|
|
export const getCanonicalPageUrl = (
|
|
site: types.Site,
|
|
recordMap: types.ExtendedRecordMap
|
|
) => (pageId = '') => {
|
|
const pageUuid = parsePageId(pageId, { uuid: true })
|
|
|
|
if (uuidToId(pageId) === site.rootNotionPageId) {
|
|
return `https://${site.domain}`
|
|
} else {
|
|
return `https://${site.domain}/${getCanonicalPageId(pageUuid, recordMap, {
|
|
uuid
|
|
})}`
|
|
}
|
|
}
|
|
|
|
function createUrl(path: string, searchParams: URLSearchParams) {
|
|
return [path, searchParams.toString()].filter(Boolean).join('?')
|
|
}
|