-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderMarkdown.tsx
More file actions
290 lines (261 loc) · 7.93 KB
/
Copy pathRenderMarkdown.tsx
File metadata and controls
290 lines (261 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import ReactMarkdown, { Components } from 'react-markdown'
import remarkGfm from 'remark-gfm'
import remarkEmoji from 'remark-emoji'
import remarkMath from 'remark-math'
import remarkBreaks from 'remark-breaks'
import rehypeKatex from 'rehype-katex'
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'
import * as prismStyles from 'react-syntax-highlighter/dist/cjs/styles/prism'
import { memo, useState, useMemo, useCallback } from 'react'
import { getReadableLanguageName } from '@/lib/utils'
import { cn } from '@/lib/utils'
import { useCodeblock } from '@/hooks/useCodeblock'
import 'katex/dist/katex.min.css'
import { IconCopy, IconCopyCheck } from '@tabler/icons-react'
import rehypeRaw from 'rehype-raw'
import { useTranslation } from '@/i18n/react-i18next-compat'
interface MarkdownProps {
content: string
className?: string
components?: Components
enableRawHtml?: boolean
isUser?: boolean
isWrapping?: boolean
}
// Cache for normalized LaTeX content
const latexCache = new Map<string, string>()
/**
* Optimized preprocessor: normalize LaTeX fragments into $ / $$.
* Uses caching to avoid reprocessing the same content.
*/
const normalizeLatex = (input: string): string => {
// Check cache first
if (latexCache.has(input)) {
return latexCache.get(input)!
}
const segments = input.split(/(```[\s\S]*?```|`[^`]*`|<[^>]+>)/g)
const result = segments
.map((segment) => {
if (!segment) return ''
// Skip code blocks, inline code, html tags
if (/^```[\s\S]*```$/.test(segment)) return segment
if (/^`[^`]*`$/.test(segment)) return segment
if (/^<[^>]+>$/.test(segment)) return segment
let s = segment
// --- Display math: \[...\] surrounded by newlines
s = s.replace(
/(^|\n)\\\[\s*\n([\s\S]*?)\n\s*\\\](?=\n|$)/g,
(_, pre, inner) => `${pre}$$\n${inner.trim()}\n$$`
)
// --- Inline math: space \( ... \)
s = s.replace(
/(^|[^$\\])\\\((.+?)\\\)(?=[^$\\]|$)/g,
(_, pre, inner) => `${pre}$${inner.trim()}$`
)
// --- Escape $<number> to prevent Markdown from treating it as LaTeX
// Example: "$1" → "\$1"
s = s.replace(/\$(\d+)/g, '\\$$1')
return s
})
.join('')
// Cache the result (with size limit to prevent memory leaks)
if (latexCache.size > 100) {
const firstKey = latexCache.keys().next().value || ''
latexCache.delete(firstKey)
}
latexCache.set(input, result)
return result
}
// Memoized code component to prevent unnecessary re-renders
const CodeComponent = memo(
({
className,
children,
isUser,
codeBlockStyle,
showLineNumbers,
isWrapping,
onCopy,
copiedId,
...props
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}: any) => {
const { t } = useTranslation()
const match = /language-(\w+)/.exec(className || '')
const language = match ? match[1] : ''
const isInline = !match || !language
const code = String(children).replace(/\n$/, '')
// Generate a stable ID based on content hash instead of position
const codeId = useMemo(() => {
let hash = 0
for (let i = 0; i < code.length; i++) {
const char = code.charCodeAt(i)
hash = (hash << 5) - hash + char
hash = hash & hash // Convert to 32-bit integer
}
return `code-${Math.abs(hash)}-${language}`
}, [code, language])
const handleCopyClick = useCallback(
(e: React.MouseEvent) => {
e.stopPropagation()
onCopy(code, codeId)
},
[code, codeId, onCopy]
)
if (isInline || isUser) {
return <code className={cn(className)}>{children}</code>
}
return (
<div className="relative overflow-hidden border rounded-md border-main-view-fg/2">
<style>
{`
.react-syntax-highlighter-line-number {
user-select: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
`}
</style>
<div className="flex items-center justify-between px-4 py-2 bg-main-view/10">
<span className="font-medium text-xs font-sans">
{getReadableLanguageName(language)}
</span>
<button
onClick={handleCopyClick}
className="flex items-center gap-1 text-xs font-sans transition-colors cursor-pointer"
>
{copiedId === codeId ? (
<>
<IconCopyCheck size={16} className="text-primary" />
<span>{t('copied')}</span>
</>
) : (
<>
<IconCopy size={16} />
<span>{t('copy')}</span>
</>
)}
</button>
</div>
<SyntaxHighlighter
style={
prismStyles[
codeBlockStyle
.split('-')
.map((part: string, index: number) =>
index === 0
? part
: part.charAt(0).toUpperCase() + part.slice(1)
)
.join('') as keyof typeof prismStyles
] || prismStyles.oneLight
}
language={language}
showLineNumbers={showLineNumbers}
wrapLines={true}
lineProps={
isWrapping
? {
style: { wordBreak: 'break-all', whiteSpace: 'pre-wrap' },
}
: {}
}
customStyle={{
margin: 0,
padding: '8px',
borderRadius: '0 0 4px 4px',
overflow: 'auto',
border: 'none',
}}
PreTag="div"
CodeTag={'code'}
{...props}
>
{code}
</SyntaxHighlighter>
</div>
)
}
)
CodeComponent.displayName = 'CodeComponent'
function RenderMarkdownComponent({
content,
enableRawHtml,
className,
isUser,
components,
isWrapping,
}: MarkdownProps) {
const { codeBlockStyle, showLineNumbers } = useCodeblock()
// State for tracking which code block has been copied
const [copiedId, setCopiedId] = useState<string | null>(null)
// Memoized copy handler
const handleCopy = useCallback((code: string, id: string) => {
navigator.clipboard.writeText(code)
setCopiedId(id)
// Reset copied state after 2 seconds
setTimeout(() => {
setCopiedId(null)
}, 2000)
}, [])
// Memoize the normalized content to avoid reprocessing on every render
const normalizedContent = useMemo(() => normalizeLatex(content), [content])
// Stable remarkPlugins reference
const remarkPlugins = useMemo(() => {
return [remarkGfm, remarkMath, remarkEmoji, remarkBreaks]
}, [])
// Stable rehypePlugins reference
const rehypePlugins = useMemo(() => {
return enableRawHtml ? [rehypeKatex, rehypeRaw] : [rehypeKatex]
}, [enableRawHtml])
// Memoized components with stable references
const markdownComponents: Components = useMemo(
() => ({
code: (props) => (
<CodeComponent
{...props}
isUser={isUser}
codeBlockStyle={codeBlockStyle}
showLineNumbers={showLineNumbers}
isWrapping={isWrapping}
onCopy={handleCopy}
copiedId={copiedId}
/>
),
// Add other optimized components if needed
...components,
}),
[
isUser,
codeBlockStyle,
showLineNumbers,
isWrapping,
handleCopy,
copiedId,
components,
]
)
// Render the markdown content
return (
<div
className={cn(
'markdown break-words select-text',
isUser && 'is-user',
className
)}
>
<ReactMarkdown
remarkPlugins={remarkPlugins}
rehypePlugins={rehypePlugins}
components={markdownComponents}
>
{normalizedContent}
</ReactMarkdown>
</div>
)
}
export const RenderMarkdown = memo(
RenderMarkdownComponent,
(prevProps, nextProps) => prevProps.content === nextProps.content
)