Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

feat(web): disable LaTeX math rendering for user messages (#19624)

chanx4122ca05610c
diff --git a/web/src/components/markdown-content/index.tsx b/web/src/components/markdown-content/index.tsx
index ae5d3bde0e..dd2cf2f7fa 100644
--- a/web/src/components/markdown-content/index.tsx
+++ b/web/src/components/markdown-content/index.tsx
@@ -16,7 +16,10 @@
import Image, { AuthenticatedImg } from '@/components/image';
import SvgIcon from '@/components/svg-icon';
-import { MarkdownRemarkPlugins } from '@/constants/markdown-remark-plugins';
+import {
+ MarkdownRemarkPlugins,
+ MarkdownRemarkPluginsLite,
+} from '@/constants/markdown-remark-plugins';
import { IReference, IReferenceChunk } from '@/interfaces/database/chat';
import { citationMarkerReg } from '@/utils/citation-utils';
import { getExtension } from '@/utils/document-util';
@@ -129,11 +132,17 @@ const MarkdownContent = ({
clickDocumentButton,
content,
loading,
+ disableMath = false,
}: {
content: string;
loading: boolean;
reference: IReference;
clickDocumentButton?: (documentId: string, chunk: IReferenceChunk) => void;
+ /**
+ * When true, disables LaTeX math rendering (remark-math + rehype-katex).
+ * Use this for user-generated content where `$` should be treated as literal text.
+ */
+ disableMath?: boolean;
}) => {
const { t } = useTranslation();
const { setDocumentIds, data: fileThumbnails } =
@@ -355,8 +364,14 @@ const MarkdownContent = ({
return (
<div dir={dir} className={styles.markdownContentWrapper}>
<Markdown
- rehypePlugins={MarkdownRehypePlugins}
- remarkPlugins={MarkdownRemarkPlugins}
+ rehypePlugins={
+ disableMath
+ ? MarkdownRehypePlugins.filter((p) => p !== rehypeKatex)
+ : MarkdownRehypePlugins
+ }
+ remarkPlugins={
+ disableMath ? MarkdownRemarkPluginsLite : MarkdownRemarkPlugins
+ }
components={markdownComponents}
>
{contentWithCursor}
diff --git a/web/src/components/message-item/index.tsx b/web/src/components/message-item/index.tsx
index a51cab3246..ffb96a0178 100644
--- a/web/src/components/message-item/index.tsx
+++ b/web/src/components/message-item/index.tsx
@@ -174,6 +174,7 @@ const MessageItem = ({
content={messageContent}
reference={reference}
clickDocumentButton={clickDocumentButton}
+ disableMath={isUser}
></MarkdownContent>
)}
</div>
diff --git a/web/src/components/next-markdown-content/index.tsx b/web/src/components/next-markdown-content/index.tsx
index acc9a6283c..9cbb7e3b09 100644
--- a/web/src/components/next-markdown-content/index.tsx
+++ b/web/src/components/next-markdown-content/index.tsx
@@ -17,7 +17,10 @@
import Image, { AuthenticatedImg } from '@/components/image';
import SvgIcon from '@/components/svg-icon';
import { SafeImg } from '@/components/safe-img';
-import { MarkdownRemarkPlugins } from '@/constants/markdown-remark-plugins';
+import {
+ MarkdownRemarkPlugins,
+ MarkdownRemarkPluginsLite,
+} from '@/constants/markdown-remark-plugins';
import { IReferenceChunk, IReferenceObject } from '@/interfaces/database/chat';
import { getExtension } from '@/utils/document-util';
import { supportsSourceLocate } from '@/utils/source-locate';
@@ -183,11 +186,17 @@ function MarkdownContent({
clickDocumentButton,
content,
loading,
+ disableMath = false,
}: {
content: string;
loading: boolean;
reference?: IReferenceObject;
clickDocumentButton?: (documentId: string, chunk: IReferenceChunk) => void;
+ /**
+ * When true, disables LaTeX math rendering (remark-math + rehype-katex).
+ * Use this for user-generated content where `$` should be treated as literal text.
+ */
+ disableMath?: boolean;
}) {
const { t } = useTranslation();
const { setDocumentIds, data: fileThumbnails } =
@@ -392,13 +401,19 @@ function MarkdownContent({
return (
<div dir={dir} className={styles.markdownContentWrapper}>
<Markdown
- rehypePlugins={[
- rehypeRaw,
- RehypeSanitizeAssistantMarkdown,
- rehypeWrapReference,
- rehypeKatex,
- ]}
- remarkPlugins={MarkdownRemarkPlugins}
+ rehypePlugins={
+ disableMath
+ ? [rehypeRaw, RehypeSanitizeAssistantMarkdown, rehypeWrapReference]
+ : [
+ rehypeRaw,
+ RehypeSanitizeAssistantMarkdown,
+ rehypeWrapReference,
+ rehypeKatex,
+ ]
+ }
+ remarkPlugins={
+ disableMath ? MarkdownRemarkPluginsLite : MarkdownRemarkPlugins
+ }
urlTransform={(url, key) => {
if (
key === 'src' &&
diff --git a/web/src/components/next-message-item/index.tsx b/web/src/components/next-message-item/index.tsx
index 2acb082dd2..f044b53259 100644
--- a/web/src/components/next-message-item/index.tsx
+++ b/web/src/components/next-message-item/index.tsx
@@ -169,6 +169,7 @@ function MessageItem({
content={messageContent}
reference={reference}
clickDocumentButton={clickDocumentButton}
+ disableMath={isUser}
></MarkdownContent>
)}
</div>