1
- import React from 'react' ;
1
+ import React , { useState , useEffect } from 'react' ;
2
2
import { Box , Text , Link as ChakraLink , Heading , useColorModeValue , useColorMode } from '@chakra-ui/react' ;
3
3
import MonacoEditor from '@monaco-editor/react' ;
4
4
import ReactMarkdown from 'react-markdown' ;
5
5
6
6
const ChakraMarkdown = React . memo < any > ( ( { content } ) => {
7
7
const { colorMode } = useColorMode ( ) ;
8
8
const monacoTheme = colorMode === 'dark' ? 'vs-dark' : 'vs-light' ;
9
-
9
+ const [ editorHeight , setEditorHeight ] = useState ( '200px' ) ;
10
10
const renderers = {
11
11
h1 : ( { children } ) => < Heading as = "h1" size = "xl" mb = { 2 } > { children } </ Heading > ,
12
12
h2 : ( { children } ) => < Heading as = "h2" size = "lg" mb = { 2 } > { children } </ Heading > ,
@@ -18,28 +18,34 @@ const ChakraMarkdown = React.memo<any>(({ content }) => {
18
18
) ,
19
19
code : ( { inline, children, className } ) => {
20
20
const language = className ? className . replace ( 'language-' , '' ) : 'plaintext' ;
21
+ const code = String ( children ) ;
21
22
if ( inline ) {
22
- return < Text as = "code" bg = { useColorModeValue ( "gray.100" , "gray.700" ) } p = "1" borderRadius = "md" > { children } </ Text > ;
23
+ return < Text as = "code" bg = { useColorModeValue ( "gray.100" , "gray.700" ) } p = "1" borderRadius = "md" > { code } </ Text > ;
23
24
}
25
+ useEffect ( ( ) => {
26
+ const lines = code . split ( '\n' ) . length ;
27
+ const lineHeight = 20 ; // Approximate height of each line in pixels
28
+ setEditorHeight ( `${ lines * lineHeight } px` ) ;
29
+ } , [ children ] ) ;
24
30
return (
25
31
< Box border = "1px solid" borderColor = { useColorModeValue ( "gray.200" , "gray.600" ) } borderRadius = "md" overflow = "hidden" my = { 2 } >
26
32
< MonacoEditor
27
- height = "200px"
33
+ height = { editorHeight }
28
34
language = { language }
29
35
theme = { monacoTheme }
30
- value = { String ( children ) . trim ( ) }
36
+ value = { code }
31
37
options = { {
32
38
readOnly : true ,
33
39
minimap : { enabled : false } ,
34
40
scrollBeyondLastLine : false ,
35
41
lineNumbers : "off" ,
42
+ scrollbar : { vertical : 'hidden' } ,
36
43
} }
37
44
/>
38
45
</ Box >
39
46
) ;
40
47
} ,
41
48
} ;
42
-
43
49
return < ReactMarkdown components = { renderers } children = { content } /> ;
44
50
} ) ;
45
51
0 commit comments