Skip to content
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

Sandpack #170

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions integrations/sandpack/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@gitbook/eslint-config/integration"]
}
17 changes: 17 additions & 0 deletions integrations/sandpack/gitbook-manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: sandpack
title: Sandpack
organization: the-delta-integrations
visibility: private
description: ''
script: ./src/index.tsx
scopes:
- space:content:read
- space:content:write
- space:metadata:read
- space:metadata:write
- space:views:read
blocks:
- id: sandpack
title: Sandpack
description: TODO Sandpack description
secrets: {}
18 changes: 18 additions & 0 deletions integrations/sandpack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "sandpack",
"private": true,
"scripts": {
"lint": "eslint ./**/*.ts*",
"typecheck": "tsc --noEmit",
"publish-integrations-staging": "gitbook publish ."
},
"devDependencies": {
"@gitbook/cli": "^0.8.0",
"@gitbook/runtime": "latest"
},
"dependencies": {
"@codesandbox/sandpack-client": "^2.6.7",
"@gitbook/api": "^0.3.0",
"static-browser-server": "^1.0.5"
}
}
26 changes: 26 additions & 0 deletions integrations/sandpack/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createComponent, createIntegration, RuntimeContext } from '@gitbook/runtime';

import { getPreviewUrl } from './sandpack';

const embedBlock = createComponent<{ url?: string }, {}, {}, RuntimeContext>({
componentId: 'sandpack',

async render(element, context) {
const previewUrl = await getPreviewUrl();

return (
<block>
<webframe
source={{
url: previewUrl,
}}
aspectRatio={16 / 9}
/>
</block>
);
},
});

export default createIntegration({
components: [embedBlock],
});
19 changes: 19 additions & 0 deletions integrations/sandpack/src/sandpack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { PreviewController } from 'static-browser-server';

export const getPreviewUrl = async () => {
const files = new Map([['index.html', 'Hello world!']]);

const controller = new PreviewController({
baseUrl: 'https://preview.sandpack-static-server.codesandbox.io',
// Function to get the file content for the server this can also be async
getFileContent: (filepath) => {
const content = files.get(filepath);
if (!content) {
throw new Error('File not found');
}
return content;
},
});

return await controller.initPreview();
};
3 changes: 3 additions & 0 deletions integrations/sandpack/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@gitbook/tsconfig/integration.json"
}
Loading