Skip to content

Commit 7dcd2d3

Browse files
committed
add image
1 parent 1877e8b commit 7dcd2d3

4 files changed

Lines changed: 63 additions & 13 deletions

File tree

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
A HonKit plugin for [Sandpack](https://sandpack.codesandbox.io/docs/).
44

5+
![img.png](img.png)
6+
57
## Installation
68

79
npm install honkit-plugin-sandpack
@@ -20,22 +22,50 @@ Via `book.json`:
2022

2123
Integration code with HTML comments.
2224

25+
2326
<!-- sandpack:{
2427
"files": {
2528
"/src/index.js": {
26-
"path": "src/index.js"
29+
"path": "example1/index.js"
2730
},
2831
"/index.html": {
29-
"path": "src/index.html"
32+
"path": "example1/index.html"
3033
}
3134
},
32-
"entry": "/src/index.js",
35+
"entry": "/index.html",
3336
"dependencies": {
3437
"uuid": "latest"
3538
}
3639
} -->
3740
```js
38-
const test = 1
41+
document.querySelector("h1").style.color = "red";
42+
```
43+
44+
More complex example.
45+
46+
47+
<!-- sandpack:{
48+
"files": {
49+
"/src/index.js": {
50+
"prependCode": "import '/src/index.css'; // Hack to load index.css\n",
51+
"path": "example2/src/index.js"
52+
},
53+
"/src/App.js": {
54+
"path": "example2/src/App.js",
55+
"active": true
56+
},
57+
"/src/index.css": {
58+
"path": "example2/src/index.css",
59+
"hidden": true
60+
},
61+
"/index.html": {
62+
"path": "example2/index.html"
63+
}
64+
},
65+
"entry": "/index.html"
66+
} -->
67+
```js
68+
// Todo App
3969
```
4070

4171
For more details, See [example](public/)

img.png

82.9 KB
Loading

src/hook-inline.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { parseComment } from "./parse-comment";
2-
import type { SandpackBundlerFile } from "@codesandbox/sandpack-client/dist/types/types";
32
import * as fs from "fs";
43
import path from "path";
4+
import { SandboxOptions } from "./sandpack";
55

66
const escapeHTMLComment = (content: string) => {
77
// It will be restored when parsing comment
@@ -20,11 +20,7 @@ export const inlineFiles = (content: string, filePath: string) => {
2020
path: importPath,
2121
appendCode,
2222
...originalProps
23-
} = entry[1] as SandpackBundlerFile & {
24-
prependCode?: string;
25-
path?: string;
26-
appendCode?: string;
27-
};
23+
} = entry[1] as SandboxOptions["files"][1];
2824

2925
const loadedCode = importPath
3026
? escapeHTMLComment(fs.readFileSync(path.resolve(baseDir, importPath), "utf-8"))

src/sandpack.tsx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
import { Sandpack, SandpackPredefinedTemplate, SandpackSetup } from "@codesandbox/sandpack-react";
22
import React from "react";
33
import { createRoot } from "react-dom/client";
4-
import { parseComment, SandboxInfo } from "./parse-comment";
4+
import { parseComment } from "./parse-comment";
55
import { t } from "./localize";
6+
import { Dependencies, SandpackBundlerFile } from "@codesandbox/sandpack-client/dist/types/types";
67

7-
export const attachToElement = (element: HTMLElement, options: SandboxInfo, isOpen: boolean = false) => {
8+
// Based: SandboxInfo
9+
export type SandboxOptions = {
10+
files: Record<
11+
string,
12+
SandpackBundlerFile & {
13+
// prepend code snippet
14+
prependCode?: string;
15+
// load the path and fill code with it
16+
// this path is based on the markdown file path
17+
path?: string;
18+
// append code snippet
19+
appendCode?: string;
20+
}
21+
>;
22+
dependencies?: Dependencies;
23+
devDependencies?: Dependencies;
24+
entry?: string;
25+
/**
26+
* What template we use, if not defined we infer the template from the dependencies or files.
27+
*/
28+
template?: string;
29+
};
30+
31+
export const attachToElement = (element: HTMLElement, options: SandboxOptions, isOpen: boolean = false) => {
832
let currentRoot: ReturnType<typeof createRoot> | null;
933
let containerElement: HTMLDivElement | null = null;
1034
const insert = (node: HTMLElement) => {
@@ -174,7 +198,7 @@ window.gitbook.events.bind("page.change", function () {
174198
updateCodeBlocs();
175199
});
176200

177-
function replaceCodeWithConsole(codeBlock: Element, options: SandboxInfo) {
201+
function replaceCodeWithConsole(codeBlock: Element, options: SandboxOptions) {
178202
const codes = codeBlock.getElementsByTagName("code");
179203
if (!codes || codes.length === 0) {
180204
return;

0 commit comments

Comments
 (0)