forked from transitive-bullshit/nextjs-notion-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoembed.ts
64 lines (55 loc) · 1.68 KB
/
oembed.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { getPageTitle, parsePageId } from 'notion-utils'
import * as config from './config'
import { getPage } from './notion'
export const oembed = async ({
url,
maxWidth,
maxHeight,
dark = false
}: {
url: string
maxWidth?: number
maxHeight?: number
dark?: boolean
}) => {
// TODO: handle pages with no pageId via domain
const pageId = parsePageId(url)
let title = config.name
let authorName = config.author
// TODO: handle errors gracefully
const page = await getPage(pageId)
const pageTitle = getPageTitle(page)
if (pageTitle) title = pageTitle
const user = page.notion_user[Object.keys(page.notion_user)[0]]?.value
const name = [user.given_name, user.family_name]
.filter(Boolean)
.join(' ')
.trim()
if (name) authorName = name
const params: any = { lite: 'true' }
if (dark) {
params.dark = 'true'
}
const query = new URLSearchParams(params).toString()
const embedUrl = `${config.host}/${pageId}?${query}`
const defaultWidth = 800
const defaultHeight = 600
const width = maxWidth ? Math.min(maxWidth, defaultWidth) : defaultWidth
const height = maxHeight ? Math.min(maxHeight, defaultHeight) : defaultHeight
return {
version: '1.0',
type: 'rich',
provider_name: config.author,
provider_url: config.host,
title,
author_name: authorName,
url,
// TODO
// thumbnail_url: 'https://repl.it/public/images/replit-logo-800x600.png',
// thumbnail_width: 800,
// thumbnail_height: 600,
width,
height,
html: `<iframe src="${embedUrl}" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts" width="${width}" height="${height}" frameborder="0"></iframe>`
}
}