balibabu
commited on
Commit
·
18e87af
1
Parent(s):
e7d6550
Fix: Fixed an issue where math formulas could not be displayed correctly #4405 (#4506)
Browse files### What problem does this PR solve?
[remarkjs/react-markdown/issues/785](https://github.com/remarkjs/react-markdown/issues/785)
Fix: Fixed an issue where math formulas could not be displayed correctly
#4405
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
web/src/components/highlight-markdown/index.tsx
CHANGED
@@ -8,6 +8,7 @@ import remarkMath from 'remark-math';
|
|
8 |
|
9 |
import 'katex/dist/katex.min.css'; // `rehype-katex` does not import the CSS for you
|
10 |
|
|
|
11 |
import styles from './index.less';
|
12 |
|
13 |
const HightLightMarkdown = ({
|
@@ -43,7 +44,7 @@ const HightLightMarkdown = ({
|
|
43 |
} as any
|
44 |
}
|
45 |
>
|
46 |
-
{children}
|
47 |
</Markdown>
|
48 |
);
|
49 |
};
|
|
|
8 |
|
9 |
import 'katex/dist/katex.min.css'; // `rehype-katex` does not import the CSS for you
|
10 |
|
11 |
+
import { preprocessLaTeX } from '@/utils/chat';
|
12 |
import styles from './index.less';
|
13 |
|
14 |
const HightLightMarkdown = ({
|
|
|
44 |
} as any
|
45 |
}
|
46 |
>
|
47 |
+
{children ? preprocessLaTeX(children) : children}
|
48 |
</Markdown>
|
49 |
);
|
50 |
};
|
web/src/pages/chat/markdown-content/index.tsx
CHANGED
@@ -20,7 +20,9 @@ import { useTranslation } from 'react-i18next';
|
|
20 |
|
21 |
import 'katex/dist/katex.min.css'; // `rehype-katex` does not import the CSS for you
|
22 |
|
|
|
23 |
import { replaceTextByOldReg } from '../utils';
|
|
|
24 |
import styles from './index.less';
|
25 |
|
26 |
const reg = /(~{2}\d+={2})/g;
|
@@ -48,7 +50,7 @@ const MarkdownContent = ({
|
|
48 |
text = t('chat.searching');
|
49 |
}
|
50 |
const nextText = replaceTextByOldReg(text);
|
51 |
-
return loading ? nextText?.concat('~~2$$') : nextText;
|
52 |
}, [content, loading, t]);
|
53 |
|
54 |
useEffect(() => {
|
|
|
20 |
|
21 |
import 'katex/dist/katex.min.css'; // `rehype-katex` does not import the CSS for you
|
22 |
|
23 |
+
import { preprocessLaTeX } from '@/utils/chat';
|
24 |
import { replaceTextByOldReg } from '../utils';
|
25 |
+
|
26 |
import styles from './index.less';
|
27 |
|
28 |
const reg = /(~{2}\d+={2})/g;
|
|
|
50 |
text = t('chat.searching');
|
51 |
}
|
52 |
const nextText = replaceTextByOldReg(text);
|
53 |
+
return loading ? nextText?.concat('~~2$$') : preprocessLaTeX(nextText);
|
54 |
}, [content, loading, t]);
|
55 |
|
56 |
useEffect(() => {
|
web/src/utils/chat.ts
CHANGED
@@ -34,3 +34,18 @@ export const buildMessageUuidWithRole = (
|
|
34 |
) => {
|
35 |
return `${message.role}_${message.id}`;
|
36 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
) => {
|
35 |
return `${message.role}_${message.id}`;
|
36 |
};
|
37 |
+
|
38 |
+
// Preprocess LaTeX equations to be rendered by KaTeX
|
39 |
+
// ref: https://github.com/remarkjs/react-markdown/issues/785
|
40 |
+
|
41 |
+
export const preprocessLaTeX = (content: string) => {
|
42 |
+
const blockProcessedContent = content.replace(
|
43 |
+
/\\\[([\s\S]*?)\\\]/g,
|
44 |
+
(_, equation) => `$$${equation}$$`,
|
45 |
+
);
|
46 |
+
const inlineProcessedContent = blockProcessedContent.replace(
|
47 |
+
/\\\(([\s\S]*?)\\\)/g,
|
48 |
+
(_, equation) => `$${equation}$`,
|
49 |
+
);
|
50 |
+
return inlineProcessedContent;
|
51 |
+
};
|