This repository was archived by the owner on Jan 24, 2025. It is now read-only.
File tree 4 files changed +34
-1
lines changed 4 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 47
47
"signale" : " ^1.3.0" ,
48
48
"strip-indent" : " ^2.0.0" ,
49
49
"to-vfile" : " ^5.0.2" ,
50
+ "unescape-js" : " ^1.1.1" ,
50
51
"unified" : " ^7.0.1" ,
51
52
"unist-util-find" : " ^1.0.1" ,
52
53
"unist-util-is" : " ^2.1.2" ,
Original file line number Diff line number Diff line change 1
1
import * as path from 'path'
2
2
import { assembleFiles } from 'codesandboxer-fs'
3
+ import unescapeJS from 'unescape-js'
3
4
4
5
import { formatter } from './format'
5
6
7
+ const classComponent = ( code : string ) => {
8
+ const match = code . match ( / ( c l a s s ) ( \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
+
6
25
const wrapCode = ( code : string ) : string =>
7
26
`import React from 'react';
8
27
@@ -21,7 +40,7 @@ const wrapCode = (code: string): string =>
21
40
22
41
export default () => (
23
42
<App>
24
- ${ code . split ( '\n' ) . join ( '\n ' ) }
43
+ ${ checkCodeToRender ( unescapeJS ( code ) ) }
25
44
</App>
26
45
)`
27
46
Original file line number Diff line number Diff line change @@ -15,4 +15,5 @@ declare module 'strip-indent'
15
15
declare module 'unist-util-visit'
16
16
declare module 'unist-util-is'
17
17
declare module 'unist-util-find'
18
+ declare module 'unescape-js'
18
19
declare module 'codesandboxer-fs'
Original file line number Diff line number Diff line change @@ -12347,6 +12347,11 @@ string-width@^1.0.1:
12347
12347
is-fullwidth-code-point "^2.0.0"
12348
12348
strip-ansi "^4.0.0"
12349
12349
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
+
12350
12355
string.prototype.padend@^3.0.0:
12351
12356
version "3.0.0"
12352
12357
resolved "https://registry.npmjs.org/string.prototype.padend/-/string.prototype.padend-3.0.0.tgz#f3aaef7c1719f170c5eab1c32bf780d96e21f2f0"
@@ -13026,6 +13031,13 @@ umask@^1.1.0:
13026
13031
resolved "https://registry.npmjs.org/umask/-/umask-1.1.0.tgz#f29cebf01df517912bb58ff9c4e50fde8e33320d"
13027
13032
integrity sha1-8pzr8B31F5ErtY/5xOUP3o4zMg0=
13028
13033
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
+
13029
13041
unescape@^0.2.0:
13030
13042
version "0.2.0"
13031
13043
resolved "https://registry.npmjs.org/unescape/-/unescape-0.2.0.tgz#b78b9b60c86f1629df181bf53eee3bc8d6367ddf"
You can’t perform that action at this time.
0 commit comments