mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-06-02 23:16:46 +00:00
feat: initial webapp structure from notion2site
This commit is contained in:
66
lib/oembed.ts
Normal file
66
lib/oembed.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { parsePageId, getPageTitle } from 'notion-utils'
|
||||
import { getPage } from './notion'
|
||||
import * as config from './env'
|
||||
|
||||
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.siteName
|
||||
let authorName = config.siteAuthor
|
||||
|
||||
try {
|
||||
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
|
||||
} catch (err) {
|
||||
// TODO: handle gracefully
|
||||
throw err
|
||||
}
|
||||
|
||||
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.siteName,
|
||||
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>`
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user