forked from transitive-bullshit/nextjs-notion-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
map-page-url.ts
43 lines (36 loc) · 1.33 KB
/
map-page-url.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { type ExtendedRecordMap } from 'notion-types'
import { parsePageId, uuidToId } from 'notion-utils'
import { includeNotionIdInUrls } from './config'
import { getCanonicalPageId } from './get-canonical-page-id'
import { type Site } from './types'
// include UUIDs in page URLs during local development but not in production
// (they're nice for debugging and speed up local dev)
const uuid = !!includeNotionIdInUrls
export const mapPageUrl =
(site: Site, recordMap: ExtendedRecordMap, searchParams: URLSearchParams) =>
(pageId = '') => {
const pageUuid = parsePageId(pageId, { uuid: true })
if (uuidToId(pageUuid) === site.rootNotionPageId) {
return createUrl('/', searchParams)
} else {
return createUrl(
`/${getCanonicalPageId(pageUuid, recordMap, { uuid })}`,
searchParams
)
}
}
export const getCanonicalPageUrl =
(site: Site, recordMap: 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('?')
}