fix: use dynamic imports for KaTeX to resolve module initialization error
Browse files- Prevents circular dependency issues that caused "Cannot accexss 'gm' before initialization" errors when rendering LaTeX formulas.
lightrag_webui/src/components/retrieval/ChatMessage.tsx
CHANGED
@@ -8,9 +8,7 @@ import ReactMarkdown from 'react-markdown'
|
|
8 |
import remarkGfm from 'remark-gfm'
|
9 |
import rehypeReact from 'rehype-react'
|
10 |
import remarkMath from 'remark-math'
|
11 |
-
import rehypeKatex from 'rehype-katex'
|
12 |
import mermaid from 'mermaid'
|
13 |
-
import 'katex/dist/katex.min.css'
|
14 |
|
15 |
import type { Element } from 'hast'
|
16 |
|
@@ -34,6 +32,23 @@ export type MessageWithError = Message & {
|
|
34 |
export const ChatMessage = ({ message }: { message: MessageWithError }) => { // Remove isComplete prop
|
35 |
const { t } = useTranslation()
|
36 |
const { theme } = useTheme()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
const handleCopyMarkdown = useCallback(async () => {
|
38 |
if (message.content) {
|
39 |
try {
|
@@ -59,11 +74,14 @@ export const ChatMessage = ({ message }: { message: MessageWithError }) => { //
|
|
59 |
className="prose dark:prose-invert max-w-none text-sm break-words prose-headings:mt-4 prose-headings:mb-2 prose-p:my-2 prose-ul:my-2 prose-ol:my-2 prose-li:my-1 [&_.katex]:text-current [&_.katex-display]:my-4 [&_.katex-display]:overflow-x-auto"
|
60 |
remarkPlugins={[remarkGfm, remarkMath]}
|
61 |
rehypePlugins={[
|
62 |
-
[
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
67 |
rehypeReact
|
68 |
]}
|
69 |
skipHtml={false}
|
|
|
8 |
import remarkGfm from 'remark-gfm'
|
9 |
import rehypeReact from 'rehype-react'
|
10 |
import remarkMath from 'remark-math'
|
|
|
11 |
import mermaid from 'mermaid'
|
|
|
12 |
|
13 |
import type { Element } from 'hast'
|
14 |
|
|
|
32 |
export const ChatMessage = ({ message }: { message: MessageWithError }) => { // Remove isComplete prop
|
33 |
const { t } = useTranslation()
|
34 |
const { theme } = useTheme()
|
35 |
+
const [katexPlugin, setKatexPlugin] = useState<any>(null)
|
36 |
+
|
37 |
+
// Load KaTeX dynamically
|
38 |
+
useEffect(() => {
|
39 |
+
const loadKaTeX = async () => {
|
40 |
+
try {
|
41 |
+
const [{ default: rehypeKatex }] = await Promise.all([
|
42 |
+
import('rehype-katex'),
|
43 |
+
import('katex/dist/katex.min.css')
|
44 |
+
])
|
45 |
+
setKatexPlugin(() => rehypeKatex)
|
46 |
+
} catch (error) {
|
47 |
+
console.error('Failed to load KaTeX:', error)
|
48 |
+
}
|
49 |
+
}
|
50 |
+
loadKaTeX()
|
51 |
+
}, [])
|
52 |
const handleCopyMarkdown = useCallback(async () => {
|
53 |
if (message.content) {
|
54 |
try {
|
|
|
74 |
className="prose dark:prose-invert max-w-none text-sm break-words prose-headings:mt-4 prose-headings:mb-2 prose-p:my-2 prose-ul:my-2 prose-ol:my-2 prose-li:my-1 [&_.katex]:text-current [&_.katex-display]:my-4 [&_.katex-display]:overflow-x-auto"
|
75 |
remarkPlugins={[remarkGfm, remarkMath]}
|
76 |
rehypePlugins={[
|
77 |
+
...(katexPlugin ? [[
|
78 |
+
katexPlugin,
|
79 |
+
{
|
80 |
+
errorColor: theme === 'dark' ? '#ef4444' : '#dc2626',
|
81 |
+
throwOnError: false,
|
82 |
+
displayMode: false
|
83 |
+
}
|
84 |
+
] as any] : []),
|
85 |
rehypeReact
|
86 |
]}
|
87 |
skipHtml={false}
|