Skip to content

Commit 32ce7f9

Browse files
committed
Playground preview
1 parent 49953ab commit 32ce7f9

File tree

7 files changed

+78
-220
lines changed

7 files changed

+78
-220
lines changed

packages/mdx/src/remark/transform.preview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fetch from "node-fetch"
21
import { visitAsync, toJSX } from "./unist-utils"
32
import { JsxNode, SuperNode } from "./nodes"
43

@@ -14,6 +13,7 @@ export async function getPresetConfig(
1413
const prefix = "https://codesandbox.io/s/"
1514
const csbid = url.slice(prefix.length)
1615
const configUrl = `https://codesandbox.io/api/v1/sandboxes/${csbid}/sandpack`
16+
const { default: fetch } = await import("node-fetch")
1717
const res = await fetch(configUrl)
1818
return await res.json()
1919
}

playground/package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@
99
},
1010
"dependencies": {
1111
"@code-hike/mdx": "^0.5.1",
12-
"@codemirror/basic-setup": "^0.20.0",
13-
"@codemirror/commands": "^0.20.0",
14-
"@codemirror/lang-markdown": "^0.20.1",
15-
"@codemirror/state": "^0.20.0",
16-
"@codemirror/view": "^0.20.5",
1712
"@mdx-js/mdx": "^2.1.1",
1813
"@monaco-editor/react": "^4.4.5",
1914
"react": "^17.0.2",
20-
"react-dom": "^17.0.2"
15+
"react-dom": "^17.0.2",
16+
"react-error-boundary": "^3.1.4"
2117
},
2218
"devDependencies": {
2319
"@types/react": "^17.0.2",

playground/src/app.jsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
1-
import { useRef, useEffect } from "react";
2-
import * as runtime from "react/jsx-runtime.js";
31
import Editor from "@monaco-editor/react";
42
import { useState } from "react";
5-
6-
import { compile, run } from "@mdx-js/mdx";
3+
import { Preview } from "./preview";
74

85
const defaultCode = `
96
# Hello
107
118
This is a markdown editor.
129
10+
<CH.Code style={{height: 200}}>
11+
1312
~~~py foo.py
1413
print "Hello, world!";
1514
~~~
1615
16+
</CH.Code>
17+
1718
`;
1819

1920
function App() {
2021
const [code, setCode] = useState(defaultCode);
21-
const [Content, setContent] = useState(undefined);
2222
function handleEditorChange(value, event) {
2323
setCode(value);
2424
}
2525

26-
useEffect(() => {
27-
compile(code, { outputFormat: "function-body" })
28-
.then((c) => run(String(c), runtime))
29-
.then((x) => setContent(x.default));
30-
}, [code]);
3126
return (
3227
<div className="app">
3328
<header>
@@ -55,7 +50,7 @@ function App() {
5550
tabSize: 2,
5651
}}
5752
/>
58-
<div className="preview">{Content}</div>
53+
<Preview code={code} />
5954
</main>
6055
</div>
6156
);

playground/src/index.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ main {
4848
border-left: 2px solid cadetblue;
4949
padding: 1em;
5050
}
51+
52+
.preview-error {
53+
border: 1px solid red;
54+
}

playground/src/preview.jsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React, { useRef, useEffect, useState } from "react";
2+
import * as runtime from "react/jsx-runtime.js";
3+
import { compile, run } from "@mdx-js/mdx";
4+
import { remarkCodeHike } from "@code-hike/mdx";
5+
import { CH } from "@code-hike/mdx/components";
6+
import "@code-hike/mdx/styles.css";
7+
import { ErrorBoundary } from "react-error-boundary";
8+
9+
export function Preview(props) {
10+
return (
11+
<div className="preview">
12+
<ErrorBoundary resetKeys={[props.code]} FallbackComponent={ErrorFallback}>
13+
<InnerPreview {...props} />
14+
</ErrorBoundary>
15+
</div>
16+
);
17+
}
18+
function ErrorFallback({ error }) {
19+
return (
20+
<div className="preview-error">
21+
<p>Something went wrong:</p>
22+
<pre>{String(error)}</pre>
23+
</div>
24+
);
25+
}
26+
27+
function InnerPreview({ code }) {
28+
const [Content, setContent] = useState(undefined);
29+
const [error, setError] = useState(undefined);
30+
useEffect(() => {
31+
compile(code, {
32+
outputFormat: "function-body",
33+
remarkPlugins: [[remarkCodeHike, { autoImport: false }]],
34+
})
35+
.then((c) => {
36+
return run(String(c), runtime);
37+
})
38+
.then((x) => {
39+
setContent(() => x.default);
40+
setError(undefined);
41+
})
42+
.catch((e) => {
43+
setError(e.message);
44+
console.error({ e });
45+
});
46+
}, [code]);
47+
// console.log(error);
48+
return (
49+
<>
50+
{Content ? Content({ components: { CH } }) : null}
51+
{error ? <div className="preview-error">{error}</div> : null}
52+
</>
53+
);
54+
}

playground/vite.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { defineConfig } from 'vite'
2-
import react from '@vitejs/plugin-react'
1+
import { defineConfig } from "vite";
2+
import react from "@vitejs/plugin-react";
33

44
// https://vitejs.dev/config/
55
export default defineConfig({
6-
plugins: [react()]
7-
})
6+
plugins: [react()],
7+
});

0 commit comments

Comments
 (0)