11import React from 'react' ;
22import ReactMarkdown from 'react-markdown' ;
33import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter' ;
4- import { vscDarkPlus as style } from 'react-syntax-highlighter/dist/esm/styles/prism' ;
4+ import {
5+ vscDarkPlus ,
6+ vs ,
7+ } from 'react-syntax-highlighter/dist/esm/styles/prism' ;
58import RemarkBreaks from 'remark-breaks' ;
69import RemarkGfm from 'remark-gfm' ;
710import RemarkMath from 'remark-math' ;
@@ -11,10 +14,12 @@ import './markdown.css';
1114import { CopyButton } from '../copy-button' ;
1215import { common , createLowlight } from 'lowlight' ;
1316import clsx from 'clsx' ;
14-
17+ import { useTheme } from 'next-themes' ;
1518const lowlight = createLowlight ( common ) ;
1619
1720const CodeBlock = ( { language, children } : any ) => {
21+ const { resolvedTheme } = useTheme ( ) ;
22+ const style = resolvedTheme === 'dark' ? vscDarkPlus : vs ;
1823 const detectedLanguage =
1924 language || lowlight . highlightAuto ( children ) . data ?. language ;
2025 return (
@@ -30,9 +35,18 @@ const CodeBlock = ({ language, children }: any) => {
3035} ;
3136
3237const InlineCode = ( { children } : any ) => (
33- < code className = "bg-base-300 p-1 rounded-md text-sm mx-1" > { children } </ code >
38+ < code className = "px-[0.3rem] py-[0.2rem] font-mono text-sm bg-muted rounded-md" >
39+ { children }
40+ </ code >
3441) ;
3542
43+ interface CodeProps extends React . HTMLAttributes < HTMLElement > {
44+ inline ?: boolean ;
45+ className ?: string ;
46+ children ?: React . ReactNode ;
47+ node ?: any ;
48+ }
49+
3650const CodeComponent = ( {
3751 node,
3852 inline,
@@ -42,11 +56,17 @@ const CodeComponent = ({
4256 ...props
4357} : any ) => {
4458 const match = / l a n g u a g e - ( \w + ) / . exec ( className || '' ) ;
45- if ( inline ) return < InlineCode { ...props } > { children } </ InlineCode > ;
59+
60+ // Handle inline code differently
61+ if ( inline ) {
62+ return < InlineCode { ...props } > { children } </ InlineCode > ;
63+ }
64+
65+ // Only wrap in div and add copy button for block code
4666 return (
4767 < div className = "relative" >
4868 < CodeBlock language = { match && match [ 1 ] } { ...props } >
49- { String ( children ) . replace ( / \n $ / , '' ) }
69+ { children }
5070 </ CodeBlock >
5171 { ! suppressCopy && (
5272 < CopyButton
@@ -58,7 +78,7 @@ const CodeComponent = ({
5878 ) ;
5979} ;
6080
61- const Markdown = ( {
81+ export const Markdown = ( {
6282 className,
6383 suppressLink,
6484 suppressCopy,
@@ -83,8 +103,26 @@ const Markdown = ({
83103 remarkPlugins = { [ RemarkGfm , RemarkBreaks , RemarkMath ] }
84104 rehypePlugins = { [ RehypeKatex ] }
85105 components = { {
86- code ( data ) : JSX . Element {
87- return < CodeComponent { ...data } suppressCopy = { suppressCopy } /> ;
106+ code ( props : any ) {
107+ const isInline =
108+ props . node ?. position ?. start . line === props . node ?. position ?. end . line ;
109+ console . log ( 'Code props:' , props , isInline ) ;
110+
111+ if ( isInline ) {
112+ return < InlineCode > { props . children } </ InlineCode > ;
113+ }
114+
115+ const match = / l a n g u a g e - ( \w + ) / . exec ( props . className || '' ) ;
116+ return (
117+ < CodeComponent
118+ inline = { false }
119+ className = { props . className }
120+ language = { match ?. [ 1 ] }
121+ { ...props }
122+ >
123+ { props . children }
124+ </ CodeComponent >
125+ ) ;
88126 } ,
89127 p ( { node, children, ...props } ) {
90128 // Replace <p> with <div>
@@ -116,5 +154,3 @@ const Markdown = ({
116154 </ ReactMarkdown >
117155 ) ;
118156} ;
119-
120- export default Markdown ;
0 commit comments