1- import { DocumentBlockEmbed } from '@gitbook/api' ;
1+ import * as gitbookAPI from '@gitbook/api' ;
22import Script from 'next/script' ;
33
44import { Card } from '@/components/primitives' ;
@@ -7,11 +7,14 @@ import { tcls } from '@/lib/tailwind';
77
88import { BlockProps } from './Block' ;
99import { Caption } from './Caption' ;
10+ import { IntegrationBlock } from './Integration' ;
1011
11- export async function Embed ( props : BlockProps < DocumentBlockEmbed > ) {
12- const { block } = props ;
12+ export async function Embed ( props : BlockProps < gitbookAPI . DocumentBlockEmbed > ) {
13+ const { block, context , ... otherProps } = props ;
1314
14- const { data : embed } = await api ( ) . urls . getEmbedByUrl ( { url : block . data . url } ) ;
15+ const { data : embed } = await ( context . content
16+ ? api ( ) . spaces . getEmbedByUrlInSpace ( context . content . spaceId , { url : block . data . url } )
17+ : api ( ) . urls . getEmbedByUrl ( { url : block . data . url } ) ) ;
1518
1619 return (
1720 < Caption { ...props } >
@@ -25,6 +28,12 @@ export async function Embed(props: BlockProps<DocumentBlockEmbed>) {
2528 { /* We load the iframely script to resize the embed iframes dynamically */ }
2629 < Script src = "https://cdn.iframe.ly/embed.js" defer async />
2730 </ >
31+ ) : embed . type === 'integration' ? (
32+ < IntegrationBlock
33+ { ...otherProps }
34+ context = { context }
35+ block = { createIntegrationBlock ( block . data . url , embed . integration , embed . block ) }
36+ />
2837 ) : (
2938 < Card
3039 leadingIcon = {
@@ -40,3 +49,28 @@ export async function Embed(props: BlockProps<DocumentBlockEmbed>) {
4049 </ Caption >
4150 ) ;
4251}
52+
53+ /**
54+ * Create an integration block with an unfurl action from the GitBook Embed response.
55+ */
56+ function createIntegrationBlock (
57+ url : string ,
58+ integration : string ,
59+ block : gitbookAPI . IntegrationBlock ,
60+ ) : gitbookAPI . DocumentBlockIntegration {
61+ return {
62+ object : 'block' ,
63+ type : 'integration' ,
64+ isVoid : true ,
65+ data : {
66+ integration,
67+ block : block . id ,
68+ props : { } ,
69+ action : {
70+ action : '@link.unfurl' ,
71+ url,
72+ } ,
73+ url,
74+ } ,
75+ } ;
76+ }
0 commit comments