forked from transitive-bullshit/nextjs-notion-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathget-canonical-page-id.ts
46 lines (40 loc) · 1.1 KB
/
get-canonical-page-id.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
44
45
46
import { ExtendedRecordMap } from 'notion-types'
import { uuidToId, getBlockTitle } from 'notion-utils'
// import {
// getCanonicalPageId as getCanonicalPageIdImpl,
// parsePageId
// } from 'notion-utils'
export const getCanonicalPageId = (
pageId: string,
recordMap: ExtendedRecordMap,
{ uuid = true }: { uuid?: boolean } = {}
): string | null => {
if (!pageId || !recordMap) return null
const id = uuidToId(pageId)
const block = recordMap.block[pageId]?.value
if (block) {
const title = normalizeTitle(getBlockTitle(block, recordMap))
if (title) {
if (uuid) {
return `${title}-${id}`
} else {
return title
}
}
}
return id
}
export const normalizeTitle = (title: string | null): string => {
return (
(title || '')
.replace(/ /g, '-')
// [한글주소지원] 대/소문자 영문/숫자가 아닌 경우 문자열 제거됨
// .replace(/[^a-zA-Z0-9-]/g, '')
.replace(/--/g, '-')
.replace(/-$/, '')
.replace(/^-/, '')
.trim()
// [한글주소지원] 소문자화 불필요
// .toLowerCase()
)
}