@@ -21,94 +21,90 @@ interface InlineHiglight {
21
21
endColumn : number ;
22
22
}
23
23
24
- const CodeBlock = React . forwardRef (
25
- (
26
- {
27
- children,
28
- className = 'language-js' ,
29
- metastring,
30
- noMargin,
31
- noMarkers,
32
- } : {
33
- children : string ;
34
- className ?: string ;
35
- metastring : string ;
36
- noMargin ?: boolean ;
37
- noMarkers ?: boolean ;
38
- } ,
39
- ref ?: React . Ref < HTMLDivElement >
40
- ) => {
41
- const getDecoratedLineInfo = ( ) => {
42
- if ( ! metastring ) {
43
- return [ ] ;
44
- }
24
+ const CodeBlock = React . forwardRef ( function CodeBlock (
25
+ {
26
+ children,
27
+ className = 'language-js' ,
28
+ metastring,
29
+ noMargin,
30
+ noMarkers,
31
+ } : {
32
+ children : string ;
33
+ className ?: string ;
34
+ metastring : string ;
35
+ noMargin ?: boolean ;
36
+ noMarkers ?: boolean ;
37
+ } ,
38
+ ref ?: React . Ref < HTMLDivElement >
39
+ ) {
40
+ const getDecoratedLineInfo = ( ) => {
41
+ if ( ! metastring ) {
42
+ return [ ] ;
43
+ }
45
44
46
- const linesToHighlight = getHighlightLines ( metastring ) ;
47
- const highlightedLineConfig = linesToHighlight . map ( ( line ) => {
48
- return {
49
- className : 'bg-github-highlight dark:bg-opacity-10' ,
50
- line,
51
- } ;
52
- } ) ;
45
+ const linesToHighlight = getHighlightLines ( metastring ) ;
46
+ const highlightedLineConfig = linesToHighlight . map ( ( line ) => {
47
+ return {
48
+ className : 'bg-github-highlight dark:bg-opacity-10' ,
49
+ line,
50
+ } ;
51
+ } ) ;
53
52
54
- const inlineHighlightLines = getInlineHighlights ( metastring , children ) ;
55
- const inlineHighlightConfig = inlineHighlightLines . map (
56
- ( line : InlineHiglight ) => ( {
57
- ...line ,
58
- elementAttributes : { 'data-step' : `${ line . step } ` } ,
59
- className : cn (
60
- 'code-step bg-opacity-10 relative rounded-md p-1 ml-2' ,
61
- {
62
- 'pl-3 before:content-[attr(data-step)] before:block before:w-4 before:h-4 before:absolute before:top-1 before:-left-2 before:rounded-full before:text-white before:text-center before:text-xs before:leading-4' : ! noMarkers ,
63
- 'bg-blue-40 before:bg-blue-40' : line . step === 1 ,
64
- 'bg-yellow-40 before:bg-yellow-40' : line . step === 2 ,
65
- 'bg-green-40 before:bg-green-40' : line . step === 3 ,
66
- 'bg-purple-40 before:bg-purple-40' : line . step === 4 ,
67
- }
68
- ) ,
69
- } )
70
- ) ;
53
+ const inlineHighlightLines = getInlineHighlights ( metastring , children ) ;
54
+ const inlineHighlightConfig = inlineHighlightLines . map (
55
+ ( line : InlineHiglight ) => ( {
56
+ ...line ,
57
+ elementAttributes : { 'data-step' : `${ line . step } ` } ,
58
+ className : cn ( 'code-step bg-opacity-10 relative rounded-md p-1 ml-2' , {
59
+ 'pl-3 before:content-[attr(data-step)] before:block before:w-4 before:h-4 before:absolute before:top-1 before:-left-2 before:rounded-full before:text-white before:text-center before:text-xs before:leading-4' :
60
+ ! noMarkers ,
61
+ 'bg-blue-40 before:bg-blue-40' : line . step === 1 ,
62
+ 'bg-yellow-40 before:bg-yellow-40' : line . step === 2 ,
63
+ 'bg-green-40 before:bg-green-40' : line . step === 3 ,
64
+ 'bg-purple-40 before:bg-purple-40' : line . step === 4 ,
65
+ } ) ,
66
+ } )
67
+ ) ;
71
68
72
- return highlightedLineConfig . concat ( inlineHighlightConfig ) ;
73
- } ;
69
+ return highlightedLineConfig . concat ( inlineHighlightConfig ) ;
70
+ } ;
74
71
75
- // e.g. "language-js"
76
- const language = className . substring ( 9 ) ;
77
- const filename = '/index.' + language ;
78
- const decorators = getDecoratedLineInfo ( ) ;
79
- return (
80
- < div
81
- translate = "no"
82
- className = { cn (
83
- 'rounded-lg h-full w-full overflow-x-auto flex items-center bg-wash dark:bg-gray-95 shadow-lg' ,
84
- ! noMargin && 'my-8'
85
- ) } >
86
- < SandpackProvider
87
- customSetup = { {
88
- entry : filename ,
89
- files : {
90
- [ filename ] : {
91
- code : children . trimEnd ( ) ,
92
- } ,
72
+ // e.g. "language-js"
73
+ const language = className . substring ( 9 ) ;
74
+ const filename = '/index.' + language ;
75
+ const decorators = getDecoratedLineInfo ( ) ;
76
+ return (
77
+ < div
78
+ translate = "no"
79
+ className = { cn (
80
+ 'rounded-lg h-full w-full overflow-x-auto flex items-center bg-wash dark:bg-gray-95 shadow-lg' ,
81
+ ! noMargin && 'my-8'
82
+ ) } >
83
+ < SandpackProvider
84
+ customSetup = { {
85
+ entry : filename ,
86
+ files : {
87
+ [ filename ] : {
88
+ code : children . trimEnd ( ) ,
93
89
} ,
94
- } } >
95
- < SandpackThemeProvider theme = { CustomTheme } >
96
- < ClasserProvider
97
- classes = { {
98
- 'sp-cm' : styles . codeViewer ,
99
- } } >
100
- < SandpackCodeViewer
101
- ref = { ref }
102
- showLineNumbers = { false }
103
- decorators = { decorators }
104
- />
105
- </ ClasserProvider >
106
- </ SandpackThemeProvider >
107
- </ SandpackProvider >
108
- </ div >
109
- ) ;
110
- }
111
- ) ;
90
+ } ,
91
+ } } >
92
+ < SandpackThemeProvider theme = { CustomTheme } >
93
+ < ClasserProvider
94
+ classes = { {
95
+ 'sp-cm' : styles . codeViewer ,
96
+ } } >
97
+ < SandpackCodeViewer
98
+ ref = { ref }
99
+ showLineNumbers = { false }
100
+ decorators = { decorators }
101
+ / >
102
+ </ ClasserProvider >
103
+ </ SandpackThemeProvider >
104
+ </ SandpackProvider >
105
+ </ div >
106
+ ) ;
107
+ } ) ;
112
108
113
109
export default CodeBlock ;
114
110
0 commit comments