|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { Children, type ComponentPropsWithoutRef, isValidElement } from 'react' |
| 4 | +import ReactMarkdown from 'react-markdown' |
| 5 | +import remarkGfm from 'remark-gfm' |
| 6 | +import 'prismjs/components/prism-typescript' |
| 7 | +import 'prismjs/components/prism-bash' |
| 8 | +import 'prismjs/components/prism-css' |
| 9 | +import 'prismjs/components/prism-markup' |
| 10 | +import '@/components/emcn/components/code/code.css' |
| 11 | +import { Checkbox, highlight, languages } from '@/components/emcn' |
| 12 | +import { cn } from '@/lib/core/utils/cn' |
| 13 | +import { useThrottledValue } from '@/hooks/use-throttled-value' |
| 14 | + |
| 15 | +const REMARK_PLUGINS = [remarkGfm] |
| 16 | + |
| 17 | +const LANG_ALIASES: Record<string, string> = { |
| 18 | + js: 'javascript', |
| 19 | + ts: 'typescript', |
| 20 | + tsx: 'typescript', |
| 21 | + jsx: 'javascript', |
| 22 | + sh: 'bash', |
| 23 | + shell: 'bash', |
| 24 | + html: 'markup', |
| 25 | + xml: 'markup', |
| 26 | + yml: 'yaml', |
| 27 | + py: 'python', |
| 28 | +} |
| 29 | + |
| 30 | +function extractTextContent(node: React.ReactNode): string { |
| 31 | + if (typeof node === 'string') return node |
| 32 | + if (typeof node === 'number') return String(node) |
| 33 | + if (!node) return '' |
| 34 | + if (Array.isArray(node)) return node.map(extractTextContent).join('') |
| 35 | + if (isValidElement(node)) |
| 36 | + return extractTextContent((node.props as { children?: React.ReactNode }).children) |
| 37 | + return '' |
| 38 | +} |
| 39 | + |
| 40 | +const PROSE_CLASSES = cn( |
| 41 | + 'prose prose-base dark:prose-invert max-w-none', |
| 42 | + 'font-[family-name:var(--font-inter)] antialiased break-words font-[420] tracking-[0]', |
| 43 | + 'prose-headings:font-[600] prose-headings:tracking-[0] prose-headings:text-[var(--text-primary)]', |
| 44 | + 'prose-headings:mb-3 prose-headings:mt-6 first:prose-headings:mt-0', |
| 45 | + 'prose-p:text-[16px] prose-p:leading-7 prose-p:text-[var(--text-primary)]', |
| 46 | + 'first:prose-p:mt-0 last:prose-p:mb-0', |
| 47 | + 'prose-li:text-[16px] prose-li:leading-7 prose-li:text-[var(--text-primary)]', |
| 48 | + 'prose-li:my-1', |
| 49 | + 'prose-ul:my-4 prose-ol:my-4', |
| 50 | + 'prose-strong:font-[600] prose-strong:text-[var(--text-primary)]', |
| 51 | + 'prose-a:text-[var(--text-primary)] prose-a:underline prose-a:decoration-dashed prose-a:underline-offset-2', |
| 52 | + 'prose-code:rounded prose-code:bg-[var(--surface-5)] prose-code:px-1.5 prose-code:py-0.5 prose-code:text-[13px] prose-code:font-mono prose-code:font-[400] prose-code:text-[var(--text-primary)]', |
| 53 | + 'prose-code:before:content-none prose-code:after:content-none', |
| 54 | + 'prose-hr:border-[var(--divider)] prose-hr:my-6', |
| 55 | + 'prose-table:my-0' |
| 56 | +) |
| 57 | + |
| 58 | +type TdProps = ComponentPropsWithoutRef<'td'> |
| 59 | +type ThProps = ComponentPropsWithoutRef<'th'> |
| 60 | + |
| 61 | +const MARKDOWN_COMPONENTS: React.ComponentProps<typeof ReactMarkdown>['components'] = { |
| 62 | + table({ children }) { |
| 63 | + return ( |
| 64 | + <div className='not-prose my-4 w-full overflow-x-auto'> |
| 65 | + <table className='min-w-full border-collapse'>{children}</table> |
| 66 | + </div> |
| 67 | + ) |
| 68 | + }, |
| 69 | + thead({ children }) { |
| 70 | + return <thead>{children}</thead> |
| 71 | + }, |
| 72 | + th({ children, style }: ThProps) { |
| 73 | + return ( |
| 74 | + <th |
| 75 | + style={style} |
| 76 | + className='whitespace-nowrap border-[var(--divider)] border-b px-3 py-2 text-left font-[600] text-[14px] text-[var(--text-primary)] leading-6' |
| 77 | + > |
| 78 | + {children} |
| 79 | + </th> |
| 80 | + ) |
| 81 | + }, |
| 82 | + td({ children, style }: TdProps) { |
| 83 | + return ( |
| 84 | + <td |
| 85 | + style={style} |
| 86 | + className='whitespace-nowrap border-[var(--divider)] border-b px-3 py-2 text-[14px] text-[var(--text-primary)] leading-6' |
| 87 | + > |
| 88 | + {children} |
| 89 | + </td> |
| 90 | + ) |
| 91 | + }, |
| 92 | + pre({ children }) { |
| 93 | + let codeString = '' |
| 94 | + let language = '' |
| 95 | + |
| 96 | + for (const child of Children.toArray(children)) { |
| 97 | + if (isValidElement(child) && child.type === 'code') { |
| 98 | + const props = child.props as { className?: string; children?: React.ReactNode } |
| 99 | + codeString = extractTextContent(props.children) |
| 100 | + if (props.className?.startsWith('language-')) { |
| 101 | + language = props.className.slice(9) |
| 102 | + } |
| 103 | + break |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (!codeString) { |
| 108 | + return ( |
| 109 | + <pre className='not-prose my-6 overflow-x-auto rounded-lg bg-[var(--surface-5)] p-4 font-[420] font-mono text-[13px] text-[var(--text-primary)] leading-[21px] dark:bg-[#1F1F1F]'> |
| 110 | + {children} |
| 111 | + </pre> |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + const resolved = LANG_ALIASES[language] || language || 'javascript' |
| 116 | + const grammar = languages[resolved] || languages.javascript |
| 117 | + const html = highlight(codeString.trimEnd(), grammar, resolved) |
| 118 | + |
| 119 | + return ( |
| 120 | + <div className='not-prose my-6 overflow-hidden rounded-lg border border-[var(--divider)]'> |
| 121 | + {language && ( |
| 122 | + <div className='border-[var(--divider)] border-b bg-[var(--surface-4)] px-4 py-2 text-[var(--text-tertiary)] text-xs dark:bg-[#2a2a2a]'> |
| 123 | + {language} |
| 124 | + </div> |
| 125 | + )} |
| 126 | + <div className='code-editor-theme bg-[var(--surface-5)] dark:bg-[#1F1F1F]'> |
| 127 | + <pre |
| 128 | + className='m-0 overflow-x-auto whitespace-pre p-4 font-[420] font-mono text-[13px] text-[var(--text-primary)] leading-[21px] dark:text-[#eeeeee]' |
| 129 | + dangerouslySetInnerHTML={{ __html: html }} |
| 130 | + /> |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + ) |
| 134 | + }, |
| 135 | + a({ children, href }) { |
| 136 | + return ( |
| 137 | + <a |
| 138 | + href={href} |
| 139 | + className='text-[var(--text-primary)] underline decoration-dashed underline-offset-2' |
| 140 | + target='_blank' |
| 141 | + rel='noopener noreferrer' |
| 142 | + > |
| 143 | + {children} |
| 144 | + </a> |
| 145 | + ) |
| 146 | + }, |
| 147 | + ul({ children, className }) { |
| 148 | + if (className?.includes('contains-task-list')) { |
| 149 | + return <ul className='my-4 list-none space-y-2 pl-0'>{children}</ul> |
| 150 | + } |
| 151 | + return <ul className='my-4 list-disc pl-5 marker:text-[var(--text-primary)]'>{children}</ul> |
| 152 | + }, |
| 153 | + ol({ children }) { |
| 154 | + return <ol className='my-4 list-decimal pl-5 marker:text-[var(--text-primary)]'>{children}</ol> |
| 155 | + }, |
| 156 | + li({ children, className }) { |
| 157 | + if (className?.includes('task-list-item')) { |
| 158 | + return ( |
| 159 | + <li className='flex list-none items-start gap-2 text-[16px] text-[var(--text-primary)] leading-7'> |
| 160 | + {children} |
| 161 | + </li> |
| 162 | + ) |
| 163 | + } |
| 164 | + return ( |
| 165 | + <li className='my-1 text-[16px] text-[var(--text-primary)] leading-7 marker:text-[var(--text-primary)]'> |
| 166 | + {children} |
| 167 | + </li> |
| 168 | + ) |
| 169 | + }, |
| 170 | + input({ type, checked }) { |
| 171 | + if (type === 'checkbox') { |
| 172 | + return <Checkbox checked={checked || false} disabled size='sm' className='mt-1.5 shrink-0' /> |
| 173 | + } |
| 174 | + return <input type={type} checked={checked} readOnly /> |
| 175 | + }, |
| 176 | +} |
| 177 | + |
| 178 | +interface ChatContentProps { |
| 179 | + content: string |
| 180 | +} |
| 181 | + |
| 182 | +export function ChatContent({ content }: ChatContentProps) { |
| 183 | + const throttled = useThrottledValue(content) |
| 184 | + return ( |
| 185 | + <div className={PROSE_CLASSES}> |
| 186 | + <ReactMarkdown remarkPlugins={REMARK_PLUGINS} components={MARKDOWN_COMPONENTS}> |
| 187 | + {throttled} |
| 188 | + </ReactMarkdown> |
| 189 | + </div> |
| 190 | + ) |
| 191 | +} |
0 commit comments