1
1
import remarkGfm from "remark-gfm" ;
2
2
import ReactMarkdown from "react-markdown" ;
3
3
import { tv } from "tailwind-variants" ;
4
- import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter' ;
5
- import { oneDark } from 'react-syntax-highlighter/dist/esm/styles/prism' ;
6
- import { CopyToClipboard } from 'react-copy-to-clipboard' ;
7
- import { ClipboardCopy } from 'lucide-react' ;
4
+ import { Prism as SyntaxHighlighter } from "react-syntax-highlighter" ;
5
+ import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism" ;
6
+ import { CopyToClipboard } from "react-copy-to-clipboard" ;
7
+ import { ClipboardCopy } from "lucide-react" ;
8
+ import { cn } from "@/lib/utils" ;
8
9
9
10
const markdown = tv ( {
10
11
base : "prose max-w-full" ,
@@ -32,49 +33,41 @@ const customStyle = {
32
33
...oneDark ,
33
34
'pre[class*="language-"]' : {
34
35
...oneDark [ 'pre[class*="language-"]' ] ,
35
- whiteSpace : ' pre-wrap' ,
36
- background : ' #1a1b26' ,
37
- padding : ' 1.5rem' ,
38
- borderRadius : ' 0.5rem' ,
39
- margin : ' 1.5rem 0' ,
40
- fontSize : ' 10px' ,
41
- width : ' 80%' , // Ensure the block takes full width
42
- position : ' relative' ,
36
+ whiteSpace : " pre-wrap" ,
37
+ background : " #1a1b26" ,
38
+ padding : " 1.5rem" ,
39
+ borderRadius : " 0.5rem" ,
40
+ margin : " 1.5rem 0" ,
41
+ fontSize : " 10px" ,
42
+ width : " 80%" , // Ensure the block takes full width
43
+ position : " relative" ,
43
44
} ,
44
45
} ;
45
46
46
-
47
47
export function Markdown ( { children, fontSize, className = "" } : Props ) {
48
48
return (
49
49
< ReactMarkdown
50
50
components = { {
51
51
/* eslint-disable @typescript-eslint/no-explicit-any */
52
52
code ( { className, children, ...props } : any ) {
53
- const match = / l a n g u a g e - ( \w + ) / . exec ( className || '' ) ;
53
+ const match = / l a n g u a g e - ( \w + ) / . exec ( className || "" ) ;
54
54
const inline = ! match ;
55
55
return ! inline ? (
56
- < div className = "relative group"
57
- style = { {
58
- maxWidth : '80%' , // Adjust this percentage to reduce the width
59
- marginLeft : '0' , // Align the box to the left
60
- paddingLeft : '1rem' ,
61
- paddingRight : '1rem' ,
62
- } }
63
- >
56
+ < div className = "relative group w-full ml-0 px-4" >
64
57
< SyntaxHighlighter
65
58
style = { {
66
59
...customStyle ,
67
60
'pre[class*="language-"]' : {
68
61
...oneDark [ 'pre[class*="language-"]' ] ,
69
- background : ' #1a1b26' ,
70
- fontSize : ' 10x' ,
71
- whiteSpace : ' pre-wrap' ,
72
- padding : ' 1.5rem' ,
73
- borderRadius : ' 0.5rem' ,
74
- margin : ' 1.5rem 0' ,
75
- position : ' relative' , // Critical for clipboard positioning
76
- width : ' 100%' , // Ensure full width of parent container
77
- boxSizing : ' border-box' , // Prevent padding overflow
62
+ background : " #1a1b26" ,
63
+ fontSize : " 10x" ,
64
+ whiteSpace : " pre-wrap" ,
65
+ padding : " 1.5rem" ,
66
+ borderRadius : " 0.5rem" ,
67
+ margin : " 1.5rem 0" ,
68
+ position : " relative" , // Critical for clipboard positioning
69
+ width : " 100%" , // Ensure full width of parent container
70
+ boxSizing : " border-box" , // Prevent padding overflow
78
71
} ,
79
72
} }
80
73
language = { match [ 1 ] }
@@ -84,9 +77,9 @@ export function Markdown({ children, fontSize, className = "" }: Props) {
84
77
wrapLines = { true }
85
78
{ ...props }
86
79
>
87
- { String ( children ) . replace ( / \n $ / , '' ) }
80
+ { String ( children ) . replace ( / \n $ / , "" ) }
88
81
</ SyntaxHighlighter >
89
- < CopyToClipboard text = { String ( children ) . replace ( / \n $ / , '' ) } >
82
+ < CopyToClipboard text = { String ( children ) . replace ( / \n $ / , "" ) } >
90
83
< button
91
84
className = "
92
85
absolute top-4 right-8 p-2 rounded-md
@@ -99,13 +92,30 @@ export function Markdown({ children, fontSize, className = "" }: Props) {
99
92
</ button >
100
93
</ CopyToClipboard >
101
94
</ div >
102
-
103
-
104
-
105
95
) : (
106
- < code className = "px-1.5 py-0.5 rounded-md bg-gray-100 text-gray-800 text-sm" { ...props } >
96
+ < SyntaxHighlighter
97
+ style = { {
98
+ ...customStyle ,
99
+ 'pre[class*="language-"]' : {
100
+ ...oneDark [ 'pre[class*="language-"]' ] ,
101
+ fontSize : "10x" ,
102
+ whiteSpace : "pre-wrap" ,
103
+ padding : "1.5rem" ,
104
+ borderRadius : "0.5rem" ,
105
+ margin : "1.5rem 0" ,
106
+ position : "relative" , // Critical for clipboard positioning
107
+ width : "100%" , // Ensure full width of parent container
108
+ boxSizing : "border-box" , // Prevent padding overflow
109
+ } ,
110
+ } }
111
+ PreTag = "div"
112
+ className = "rounded-lg overflow-hidden shadow-lg text-sm"
113
+ showLineNumbers = { false }
114
+ wrapLines = { true }
115
+ { ...props }
116
+ >
107
117
{ children }
108
- </ code >
118
+ </ SyntaxHighlighter >
109
119
) ;
110
120
} ,
111
121
pre ( { children } ) {
@@ -115,27 +125,35 @@ export function Markdown({ children, fontSize, className = "" }: Props) {
115
125
return < h1 className = "text-3xl font-bold mb-6" > { children } </ h1 > ;
116
126
} ,
117
127
h2 ( { children } ) {
118
- return < h2 className = "text-2xl font-semibold mt-8 mb-4" > { children } </ h2 > ;
128
+ return (
129
+ < h2 className = "text-2xl font-semibold mt-8 mb-4" > { children } </ h2 >
130
+ ) ;
119
131
} ,
120
132
h3 ( { children } ) {
121
133
return < h3 className = "text-xl font-medium mt-6 mb-3" > { children } </ h3 > ;
122
134
} ,
123
- p ( { children } ) {
135
+ p ( { children } ) {
124
136
return (
125
137
< p className = { cn ( "text-gray-600 leading-relaxed mb-4" , className ) } >
126
138
{ children }
127
139
</ p >
128
140
) ;
129
141
} ,
130
142
ul ( { children } ) {
131
- return < ul className = "list-disc list-inside mb-4 space-y-2" > { children } </ ul > ;
143
+ return (
144
+ < ul className = "list-disc list-inside mb-4 space-y-2" > { children } </ ul >
145
+ ) ;
132
146
} ,
133
147
li ( { children } ) {
134
148
return < li className = "text-gray-600" > { children } </ li > ;
135
149
} ,
136
150
a ( { children, ...props } ) {
137
151
return (
138
- < a className = "text-blue-600 hover:text-blue-800 underline" target = "_blank" { ...props } >
152
+ < a
153
+ className = "text-blue-600 hover:text-blue-800 underline"
154
+ target = "_blank"
155
+ { ...props }
156
+ >
139
157
{ children }
140
158
</ a >
141
159
) ;
0 commit comments