Skip to content

Commit 035b401

Browse files
authored
Use the text/html mimebundle (#27)
* Use the existing `text/html` mimebundle * fix link * fix readme * defaults * fix rendering * bump p5
1 parent c493463 commit 035b401

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# jupyterlite-p5-kernel
22

33
[![Github Actions Status](https://github.com/jupyterlite/p5-kernel/workflows/Build/badge.svg)](https://github.com/jupyterlite/p5-kernel/actions/workflows/build.yml)
4-
[![JupyterLite](https://jupyterlite.rtfd.io/en/latest/_static/badge-launch.svg)](https://jupyterlite-p5-kernel.readthedocs.io/en/latest/lite/lab)
4+
[![JupyterLite](https://jupyterlite.rtfd.io/en/latest/_static/badge-launch.svg)](https://jupyterlite.github.io/p5-kernel)
55

66
A p5.js kernel for JupyterLite.
77

@@ -23,8 +23,6 @@ pip install jupyterlite-p5-kernel
2323
jupyter lite build
2424
```
2525

26-
See the JupyterLite documentation for more information on how to build sites and include additional extensions: https://jupyterlite.readthedocs.io/en/latest/howto/index.html
27-
2826
## Uninstall
2927

3028
```bash

packages/p5-kernel-extension/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import p5Logo from '../style/icons/p5js.png';
1919
/**
2020
* The default CDN fallback for p5.js
2121
*/
22-
const P5_CDN_URL = 'https://cdn.jsdelivr.net/npm/p5@1.5.0/lib/p5.js';
22+
const P5_CDN_URL = 'https://cdn.jsdelivr.net/npm/p5@1.9.0/lib/p5.js';
2323

2424
/**
2525
* A plugin to register the p5.js kernel.

packages/p5-kernel/src/kernel.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ import { BaseKernel, type IKernel } from '@jupyterlite/services';
44

55
import { PromiseDelegate } from '@lumino/coreutils';
66

7-
/**
8-
* The mimetype for mime bundle results
9-
*/
10-
const MIME_TYPE = 'text/html-sandboxed';
11-
127
/**
138
* A kernel for making p5 sketches in the browser
149
*/
@@ -158,7 +153,6 @@ export class P5Kernel extends BaseKernel {
158153
const magics = await this._magics();
159154
const { data, metadata } = magics;
160155
this._parentHeaders.forEach(h => {
161-
this.clearOutput({ wait: false });
162156
this.updateDisplayData(
163157
{
164158
data,
@@ -290,23 +284,27 @@ export class P5Kernel extends BaseKernel {
290284
// add metadata
291285
const re = /^%show(?: (.+)\s+(.+))?\s*$/;
292286
const matches = code.match(re);
293-
const width = matches?.[1] ?? undefined;
294-
const height = matches?.[2] ?? undefined;
287+
const width = matches?.[1] ?? '100%';
288+
const height = matches?.[2] ?? '400px';
289+
// Properly escape the srcdoc content
290+
const srcdocContent = [
291+
'<body style="overflow: hidden; margin: 0; padding: 0;">',
292+
`<script>${script}</script>`,
293+
'</body>'
294+
].join('');
295+
296+
// Escape the srcdoc attribute value
297+
const escapedSrcdoc = srcdocContent
298+
.replace(/&/g, '&amp;')
299+
.replace(/'/g, '&#39;')
300+
.replace(/"/g, '&quot;');
301+
295302
return {
296303
execution_count: this.executionCount,
297304
data: {
298-
[MIME_TYPE]: [
299-
'<body style="overflow: hidden;">',
300-
`<script>${script}</script>`,
301-
'</body>'
302-
].join('\n')
305+
'text/html': `<iframe width="${width}" height="${height}" frameborder="0" srcdoc="${escapedSrcdoc}"></iframe>`
303306
},
304-
metadata: {
305-
[MIME_TYPE]: {
306-
width,
307-
height
308-
}
309-
}
307+
metadata: {}
310308
};
311309
}
312310

0 commit comments

Comments
 (0)