Skip to content

Commit

Permalink
Render Embed blocks that can be handled by an integration in the Spac…
Browse files Browse the repository at this point in the history
  • Loading branch information
emmerich authored Apr 25, 2024
1 parent f8d148b commit 5db3cbc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"dependencies": {
"@geist-ui/icons": "^1.0.2",
"@gitbook/api": "^0.43.0",
"@gitbook/api": "^0.46.0",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-popover": "^1.0.7",
"@sentry/nextjs": "^7.94.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-contentkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"exports": "./src/index.ts",
"dependencies": {
"classnames": "^2.5.1",
"@gitbook/api": "^0.41.0",
"@gitbook/api": "^0.46.0",
"assert-never": "^1.2.1"
},
"peerDependencies": {
Expand Down
42 changes: 38 additions & 4 deletions src/components/DocumentView/Embed.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DocumentBlockEmbed } from '@gitbook/api';
import * as gitbookAPI from '@gitbook/api';
import Script from 'next/script';

import { Card } from '@/components/primitives';
Expand All @@ -7,11 +7,14 @@ import { tcls } from '@/lib/tailwind';

import { BlockProps } from './Block';
import { Caption } from './Caption';
import { IntegrationBlock } from './Integration';

export async function Embed(props: BlockProps<DocumentBlockEmbed>) {
const { block } = props;
export async function Embed(props: BlockProps<gitbookAPI.DocumentBlockEmbed>) {
const { block, context, ...otherProps } = props;

const { data: embed } = await api().urls.getEmbedByUrl({ url: block.data.url });
const { data: embed } = await (context.content
? api().spaces.getEmbedByUrlInSpace(context.content.spaceId, { url: block.data.url })
: api().urls.getEmbedByUrl({ url: block.data.url }));

return (
<Caption {...props}>
Expand All @@ -25,6 +28,12 @@ export async function Embed(props: BlockProps<DocumentBlockEmbed>) {
{/* We load the iframely script to resize the embed iframes dynamically */}
<Script src="https://cdn.iframe.ly/embed.js" defer async />
</>
) : embed.type === 'integration' ? (
<IntegrationBlock
{...otherProps}
context={context}
block={createIntegrationBlock(block.data.url, embed.integration, embed.block)}
/>
) : (
<Card
leadingIcon={
Expand All @@ -40,3 +49,28 @@ export async function Embed(props: BlockProps<DocumentBlockEmbed>) {
</Caption>
);
}

/**
* Create an integration block with an unfurl action from the GitBook Embed response.
*/
function createIntegrationBlock(
url: string,
integration: string,
block: gitbookAPI.IntegrationBlock,
): gitbookAPI.DocumentBlockIntegration {
return {
object: 'block',
type: 'integration',
isVoid: true,
data: {
integration,
block: block.id,
props: {},
action: {
action: '@link.unfurl',
url,
},
url,
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export async function IntegrationBlock(props: BlockProps<DocumentBlockIntegratio
const initialInput = {
componentId: block.data.block,
props: block.data.props,
action: block.data.action,
context: contentKitContext,
};

Expand Down

0 comments on commit 5db3cbc

Please sign in to comment.