1
- import React , { useState } from 'react' ;
1
+ import React , { useEffect , useState } from 'react' ;
2
2
import { CopyToClipboard } from 'react-copy-to-clipboard' ;
3
3
import { MdContentCopy } from "react-icons/md" ;
4
4
import Markdown from 'react-markdown' ;
@@ -12,10 +12,10 @@ const CodeWithCopyButton: React.FC<CodeWithCopyButtonProps> = ({ text }) => {
12
12
const [ isCopied , setIsCopied ] = useState < boolean > ( false ) ;
13
13
const [ isButtonDisabled , setIsButtonDisabled ] = useState < boolean > ( false ) ;
14
14
15
- // Function to clean up the text before copying
15
+ // Function to clean up the text before displaying
16
16
const cleanUpText = ( text : string ) => {
17
- // Replace multiple newlines with a single newline
18
- return text . replace ( / \n { 2 , } / g, '\n ' ) ;
17
+ // Make sure that backslashes are properly escaped for Markdown
18
+ return text . replace ( / \\ / g, '\\\\ ' ) ;
19
19
} ;
20
20
21
21
const handleCopyClick = ( ) => {
@@ -27,12 +27,25 @@ const CodeWithCopyButton: React.FC<CodeWithCopyButtonProps> = ({ text }) => {
27
27
setIsButtonDisabled ( false ) ;
28
28
} ;
29
29
30
+ useEffect ( ( ) => {
31
+ let timer : NodeJS . Timeout ;
32
+ if ( isCopied ) {
33
+ timer = setTimeout ( ( ) => {
34
+ setIsCopied ( false ) ;
35
+ setIsButtonDisabled ( false ) ;
36
+ } , 2000 ) ; // Reset after 2 seconds
37
+ }
38
+ return ( ) => clearTimeout ( timer ) ;
39
+ } , [ isCopied ] ) ;
40
+
30
41
return (
31
42
< div className = 'relative border p-4 my-4' >
32
- < Markdown remarkPlugins = { [ remarkGfm ] } >
33
- { text }
34
- </ Markdown >
35
- < CopyToClipboard text = { cleanUpText ( text ) } onCopy = { handleCopy } >
43
+ < pre style = { { whiteSpace : 'pre-wrap' } } >
44
+ < Markdown remarkPlugins = { [ remarkGfm ] } >
45
+ { cleanUpText ( text ) }
46
+ </ Markdown >
47
+ </ pre >
48
+ < CopyToClipboard text = { text } onCopy = { handleCopy } >
36
49
< button
37
50
className = "absolute top-0 right-0 p-1 cursor-pointer bg-transparent border-none"
38
51
onClick = { handleCopyClick }
0 commit comments