Skip to content

feat(asset): support external video sources with Notion-style rendering #651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/notion-types/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export interface BaseContentBlock extends BaseBlock {
caption?: Decoration[]
}
format?: {
link_title: string
block_alignment: 'center' | 'left' | 'right'
block_width: number
block_height: number
Expand Down Expand Up @@ -359,6 +360,7 @@ export interface GoogleDriveBlock extends BaseContentBlock {
user_name: string
modified_time: number
}
link_title: string
block_alignment: 'center' | 'left' | 'right'
block_width: number
block_height: number
Expand Down
49 changes: 35 additions & 14 deletions packages/react-notion-x/src/components/asset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { type BaseContentBlock, type Block } from 'notion-types'
import { getTextContent } from 'notion-utils'

import { useNotionContext } from '../context'
import { getUrlParams, getYoutubeId } from '../utils'
import {
deleteUrlParams,
getUrlParams,
getYoutubeId,
setUrlParams
} from '../utils'
import { LazyImage } from './lazy-image'
import { LiteYouTubeEmbed } from './lite-youtube-embed'

Expand Down Expand Up @@ -135,9 +140,7 @@ export function Asset({
}

if (block.space_id) {
const url = new URL(source)
url.searchParams.set('spaceId', block.space_id)
source = url.toString()
source = setUrlParams(source, { spaceId: block.space_id })
}

let content = null
Expand Down Expand Up @@ -187,6 +190,7 @@ export function Asset({
block.type === 'drive' ||
block.type === 'replit'
) {
let displaySource = block.format?.display_source
if (
block.type === 'video' &&
source &&
Expand All @@ -200,19 +204,36 @@ export function Asset({
!source.includes('tella')
) {
style.paddingBottom = undefined

const videoTitle = block.format?.link_title || 'block.type'
if (displaySource) {
delete style.height
displaySource = deleteUrlParams(displaySource, ['spaceId'])
}
content = (
<video
playsInline
controls
preload='metadata'
style={assetStyle}
src={source}
title={block.type}
/>
<>
{displaySource ? (
<iframe
src={displaySource}
title={videoTitle}
style={{
...assetStyle,
aspectRatio: 1 / (block.format?.block_aspect_ratio || 1)
}}
/>
) : (
<video
playsInline
controls
preload='metadata'
style={assetStyle}
src={source}
title={videoTitle}
/>
)}
</>
)
} else {
let src = block.format?.display_source || source
let src = displaySource || source

if (src) {
const youtubeVideoId: string | null =
Expand Down
6 changes: 2 additions & 4 deletions packages/react-notion-x/src/components/audio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type React from 'react'
import { type AudioBlock } from 'notion-types'

import { useNotionContext } from '../context'
import { cs } from '../utils'
import { cs, setUrlParams } from '../utils'

export function Audio({
block,
Expand All @@ -21,9 +21,7 @@ export function Audio({
}

if (block.space_id) {
const url = new URL(source)
url.searchParams.set('spaceId', block.space_id)
source = url.toString()
source = setUrlParams(source, { spaceId: block.space_id })
}

return (
Expand Down
6 changes: 2 additions & 4 deletions packages/react-notion-x/src/components/file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type FileBlock } from 'notion-types'

import { useNotionContext } from '../context'
import { FileIcon } from '../icons/file-icon'
import { cs } from '../utils'
import { cs, setUrlParams } from '../utils'
import { Text } from './text'

export function File({
Expand All @@ -23,9 +23,7 @@ export function File({
}

if (block.space_id) {
const url = new URL(source)
url.searchParams.set('spaceId', block.space_id)
source = url.toString()
source = setUrlParams(source, { spaceId: block.space_id })
}

return (
Expand Down
19 changes: 19 additions & 0 deletions packages/react-notion-x/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,22 @@ export const getUrlParams = (

return
}

export const setUrlParams = (
urlString: string,
params: Record<string, string>
): string => {
const url = new URL(urlString)
for (const [key, value] of Object.entries(params)) {
url.searchParams.set(key, value)
}
return url.toString()
}

export const deleteUrlParams = (urlString: string, keys: string[]): string => {
const url = new URL(urlString)
for (const key of keys) {
url.searchParams.delete(key)
}
return url.toString()
}