Skip to content

Commit c386595

Browse files
author
Theodore Li
committed
Handle react node case for copy
1 parent 0413d87 commit c386595

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

apps/sim/app/chat/components/message/components/markdown-renderer.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import React, { type HTMLAttributes, memo, type ReactNode, useMemo } from 'react'
1+
import React, { type HTMLAttributes, isValidElement, memo, type ReactNode, useMemo } from 'react'
22
import ReactMarkdown from 'react-markdown'
33
import remarkGfm from 'remark-gfm'
44
import { Tooltip } from '@/components/emcn'
55
import { CopyCodeButton } from '@/components/ui/copy-code-button'
66

7+
function extractTextContent(node: ReactNode): string {
8+
if (typeof node === 'string') return node
9+
if (typeof node === 'number') return String(node)
10+
if (!node) return ''
11+
if (Array.isArray(node)) return node.map(extractTextContent).join('')
12+
if (isValidElement(node))
13+
return extractTextContent((node.props as { children?: ReactNode }).children)
14+
return ''
15+
}
16+
717
export function LinkWithPreview({ href, children }: { href: string; children: React.ReactNode }) {
818
return (
919
<Tooltip.Root delayDuration={300}>
@@ -104,7 +114,7 @@ function createCustomComponents(LinkComponent: typeof LinkWithPreview) {
104114
{codeProps.className?.replace('language-', '') || 'code'}
105115
</span>
106116
<CopyCodeButton
107-
code={typeof codeContent === 'string' ? codeContent : String(codeContent ?? '')}
117+
code={extractTextContent(codeContent)}
108118
className='text-gray-400 hover:bg-gray-700 hover:text-gray-200'
109119
/>
110120
</div>

0 commit comments

Comments
 (0)