1
- import React from ' react'
2
- import Highlight , { defaultProps } from ' prism-react-renderer'
3
- import DraculaTheme from ' prism-react-renderer/themes/dracula'
4
- import { LiveProvider , LiveEditor , LiveError , LivePreview } from ' react-live'
5
- import Prism from ' prismjs/components/prism-core' ;
1
+ import React from " react" ;
2
+ import Highlight , { defaultProps , Language } from " prism-react-renderer" ;
3
+ import DraculaTheme from " prism-react-renderer/themes/dracula" ;
4
+ import { LiveProvider , LiveEditor , LiveError , LivePreview } from " react-live" ;
5
+ import Prism from " prismjs/components/prism-core" ;
6
6
7
- Prism . languages . graphql = { comment : / # .* / , string : { pattern : / " (?: \\ .| [ ^ \\ " \r \n ] ) * " / , greedy : ! 0 } , number : / (?: \B - | \b ) \d + (?: \. \d + ) ? (?: e [ + - ] ? \d + ) ? \b / i, "boolean" : / \b (?: t r u e | f a l s e ) \b / , variable : / \$ [ a - z _ ] \w * / i, directive : { pattern : / @ [ a - z _ ] \w * / i, alias : "function" } , "attr-name" : / [ a - z _ ] \w * (? = \s * (?: \( [ ^ ( ) ] * \) ) ? : ) / i, keyword : [ { pattern : / ( f r a g m e n t \s + (? ! o n ) [ a - z _ ] \w * \s + | \. { 3 } \s * ) o n \b / , lookbehind : ! 0 } , / \b (?: q u e r y | f r a g m e n t | m u t a t i o n ) \b / ] , operator : / ! | = | \. { 3 } / , punctuation : / [ ! ( ) { } \[ \] : = , ] / } ;
7
+ Prism . languages . graphql = {
8
+ comment : / # .* / ,
9
+ string : { pattern : / " (?: \\ .| [ ^ \\ " \r \n ] ) * " / , greedy : ! 0 } ,
10
+ number : / (?: \B - | \b ) \d + (?: \. \d + ) ? (?: e [ + - ] ? \d + ) ? \b / i,
11
+ boolean : / \b (?: t r u e | f a l s e ) \b / ,
12
+ variable : / \$ [ a - z _ ] \w * / i,
13
+ directive : { pattern : / @ [ a - z _ ] \w * / i, alias : "function" } ,
14
+ "attr-name" : / [ a - z _ ] \w * (? = \s * (?: \( [ ^ ( ) ] * \) ) ? : ) / i,
15
+ keyword : [
16
+ { pattern : / ( f r a g m e n t \s + (? ! o n ) [ a - z _ ] \w * \s + | \. { 3 } \s * ) o n \b / , lookbehind : ! 0 } ,
17
+ / \b (?: q u e r y | f r a g m e n t | m u t a t i o n ) \b /
18
+ ] ,
19
+ operator : / ! | = | \. { 3 } / ,
20
+ punctuation : / [ ! ( ) { } \[ \] : = , ] /
21
+ } ;
8
22
9
- export const Code = ( props ) => {
10
- return < div style = { { position : "relative" } } >
11
- < CodeRenderer { ...props } />
12
- < button
13
- style = { { position : "absolute" , top : 10 , right : 10 } }
14
- onClick = { ( ) => {
15
- // Copying code here!
16
- } }
17
- >
18
- Copy
19
- </ button >
20
- </ div >
21
- }
23
+ type CodeProps = {
24
+ "react-live" : boolean ;
25
+ codeString : string ;
26
+ language : Language ;
27
+ } ;
22
28
23
- const CodeRenderer = ( { codeString, language, ...props } ) => {
24
- console . log ( codeString , language ) ;
25
- if ( props [ 'react-live' ] ) {
29
+ export const Code : React . FunctionComponent < CodeProps > = props => {
30
+ return (
31
+ < div style = { { position : "relative" } } >
32
+ < CodeRenderer { ...props } />
33
+ < button
34
+ style = { { position : "absolute" , top : 10 , right : 10 } }
35
+ onClick = { ( ) => {
36
+ // Copying code here!
37
+ } }
38
+ >
39
+ Copy
40
+ </ button >
41
+ </ div >
42
+ ) ;
43
+ } ;
44
+
45
+ const CodeRenderer : React . FunctionComponent < CodeProps > = ( {
46
+ codeString,
47
+ language,
48
+ ...props
49
+ } ) => {
50
+ if ( props [ "react-live" ] ) {
26
51
return (
27
52
< LiveProvider code = { codeString } noInline = { true } >
28
53
< LiveEditor />
29
54
< LiveError />
30
55
< LivePreview />
31
56
</ LiveProvider >
32
- )
57
+ ) ;
33
58
} else {
34
59
return (
35
- < Highlight { ...defaultProps } theme = { DraculaTheme } Prism = { Prism } code = { codeString } language = { language } >
60
+ < Highlight
61
+ { ...defaultProps }
62
+ theme = { DraculaTheme }
63
+ Prism = { Prism }
64
+ code = { codeString }
65
+ language = { language }
66
+ >
36
67
{ ( { className, style, tokens, getLineProps, getTokenProps } ) => (
37
68
< pre className = { className } style = { style } >
38
69
{ tokens . map ( ( line , i ) => (
@@ -45,6 +76,6 @@ const CodeRenderer = ({ codeString, language, ...props }) => {
45
76
</ pre >
46
77
) }
47
78
</ Highlight >
48
- )
79
+ ) ;
49
80
}
50
- }
81
+ } ;
0 commit comments