Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 33dc6e5

Browse files
committed
fix(docz-utils): resolve codesandbox code
1 parent cede436 commit 33dc6e5

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

packages/docz-utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"signale": "^1.3.0",
4848
"strip-indent": "^2.0.0",
4949
"to-vfile": "^5.0.2",
50+
"unescape-js": "^1.1.1",
5051
"unified": "^7.0.1",
5152
"unist-util-find": "^1.0.1",
5253
"unist-util-is": "^2.1.2",

packages/docz-utils/src/codesandbox.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
import * as path from 'path'
22
import { assembleFiles } from 'codesandboxer-fs'
3+
import unescapeJS from 'unescape-js'
34

45
import { formatter } from './format'
56

7+
const classComponent = (code: string) => {
8+
const match = code.match(/(class)(\s)(\w+)/m)
9+
const component = match && match[3]
10+
11+
return `
12+
{() => {
13+
${code}
14+
return <${component} />
15+
}}
16+
`
17+
}
18+
19+
const checkCodeToRender = (code: string) => {
20+
if (code.startsWith('()')) return `{${code}}`
21+
if (code.startsWith('class')) return classComponent(code)
22+
return `<React.Fragment>${code}</React.Fragment>`
23+
}
24+
625
const wrapCode = (code: string): string =>
726
`import React from 'react';
827
@@ -21,7 +40,7 @@ const wrapCode = (code: string): string =>
2140
2241
export default () => (
2342
<App>
24-
${code.split('\n').join('\n ')}
43+
${checkCodeToRender(unescapeJS(code))}
2544
</App>
2645
)`
2746

packages/docz-utils/src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ declare module 'strip-indent'
1515
declare module 'unist-util-visit'
1616
declare module 'unist-util-is'
1717
declare module 'unist-util-find'
18+
declare module 'unescape-js'
1819
declare module 'codesandboxer-fs'

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12347,6 +12347,11 @@ string-width@^1.0.1:
1234712347
is-fullwidth-code-point "^2.0.0"
1234812348
strip-ansi "^4.0.0"
1234912349

12350+
string.fromcodepoint@^0.2.1:
12351+
version "0.2.1"
12352+
resolved "https://registry.npmjs.org/string.fromcodepoint/-/string.fromcodepoint-0.2.1.tgz#8d978333c0bc92538f50f383e4888f3e5619d653"
12353+
integrity sha1-jZeDM8C8klOPUPOD5IiPPlYZ1lM=
12354+
1235012355
string.prototype.padend@^3.0.0:
1235112356
version "3.0.0"
1235212357
resolved "https://registry.npmjs.org/string.prototype.padend/-/string.prototype.padend-3.0.0.tgz#f3aaef7c1719f170c5eab1c32bf780d96e21f2f0"
@@ -13026,6 +13031,13 @@ umask@^1.1.0:
1302613031
resolved "https://registry.npmjs.org/umask/-/umask-1.1.0.tgz#f29cebf01df517912bb58ff9c4e50fde8e33320d"
1302713032
integrity sha1-8pzr8B31F5ErtY/5xOUP3o4zMg0=
1302813033

13034+
unescape-js@^1.1.1:
13035+
version "1.1.1"
13036+
resolved "https://registry.npmjs.org/unescape-js/-/unescape-js-1.1.1.tgz#a4345e654b857c29fa66469e311ccaf2e93063bd"
13037+
integrity sha512-2/6CdybfFt9fzYJhCD6SHfBnqCGNfjhMwPK9Pf+sJRloa/WmyAmxdBVOslOIYkvSIRKX+9xGePF5t1tugtZ63g==
13038+
dependencies:
13039+
string.fromcodepoint "^0.2.1"
13040+
1302913041
unescape@^0.2.0:
1303013042
version "0.2.0"
1303113043
resolved "https://registry.npmjs.org/unescape/-/unescape-0.2.0.tgz#b78b9b60c86f1629df181bf53eee3bc8d6367ddf"

0 commit comments

Comments
 (0)