Skip to content

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

Fix: The greater-than sign in the message cannot be displayed. (#21055)

balibabufede451994fa
diff --git a/web/src/components/markdown-content/__tests__/index.test.tsx b/web/src/components/markdown-content/__tests__/index.test.tsx
new file mode 100644
index 0000000000..2e79db70e9
--- /dev/null
+++ b/web/src/components/markdown-content/__tests__/index.test.tsx
@@ -0,0 +1,77 @@
+import { render } from '@testing-library/react';
+import React from 'react';
+
+import MarkdownContent from '..';
+
+jest.mock('react-i18next', () => ({
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+
+jest.mock('@/hooks/use-document-request', () => ({
+ useFetchDocumentThumbnailsByIds: () => ({
+ setDocumentIds: jest.fn(),
+ data: {},
+ }),
+}));
+
+jest.mock('@/hooks/use-loading-pause', () => ({
+ useLoadingPause: () => false,
+}));
+
+// Render the highlighter as a plain <pre>: the assertion targets the code
+// string the component hands down, not the highlighting itself.
+jest.mock('react-syntax-highlighter', () => ({
+ __esModule: true,
+ default: ({ children }: any) => {
+ const ReactLib = jest.requireActual('react');
+ return ReactLib.createElement('pre', null, children);
+ },
+}));
+
+// react-markdown and the remark/rehype packages are ESM-only and jest does
+// not transform node_modules, so the real parser cannot run here. This mock
+// reproduces the one behavior the fix depends on: for code content the
+// parser passes the source string through literally, entities intact.
+jest.mock('react-markdown', () => ({
+ __esModule: true,
+ default: ({ children, components }: any) => {
+ const ReactLib = jest.requireActual('react');
+ return ReactLib.createElement(
+ 'pre',
+ null,
+ ReactLib.createElement(
+ components.code,
+ { className: 'language-text' },
+ String(children),
+ ),
+ );
+ },
+}));
+
+jest.mock('rehype-katex', () => jest.fn());
+jest.mock('rehype-raw', () => jest.fn());
+jest.mock('unist-util-visit-parents', () => ({ visitParents: jest.fn() }));
+jest.mock('@/constants/markdown-remark-plugins', () => ({
+ MarkdownRemarkPlugins: [],
+ MarkdownRemarkPluginsLite: [],
+}));
+jest.mock('@/constants/markdown-rehype-plugins', () => ({
+ RehypeSanitizeAssistantMarkdown: jest.fn(),
+}));
+
+describe('MarkdownContent', () => {
+ it('renders > in code blocks instead of the &gt; entity', () => {
+ // The message pipeline escapes standalone angle brackets and restores
+ // them as entities; code content must decode them back at render time.
+ const { container } = render(
+ <MarkdownContent
+ content={'```text\n│ 12,000 > 5,000 → 同意并向上推送\n```'}
+ loading={false}
+ reference={undefined as any}
+ />,
+ );
+
+ expect(container.textContent).toContain('12,000 > 5,000');
+ expect(container.textContent).not.toContain('&gt;');
+ });
+});
diff --git a/web/src/components/markdown-content/index.tsx b/web/src/components/markdown-content/index.tsx
index 2886660350..eda6e28193 100644
--- a/web/src/components/markdown-content/index.tsx
+++ b/web/src/components/markdown-content/index.tsx
@@ -41,6 +41,7 @@ import 'katex/dist/katex.min.css'; // `rehype-katex` does not import the CSS for
import { useFetchDocumentThumbnailsByIds } from '@/hooks/use-document-request';
import { useLoadingPause } from '@/hooks/use-loading-pause';
import {
+ decodeAngleBracketEntities,
escapeUnmatchedAngleBrackets,
parseCitationIndex,
preprocessLaTeX,
@@ -103,6 +104,9 @@ const MarkdownCode = (props: any) => {
const { children, className, ...rest } = props;
const restProps = omit(rest, 'node');
const match = /language-(\w+)/.exec(className || '');
+ // Code content is literal for react-markdown, so the &lt;/&gt; entities
+ // restored by unescapeAngleBrackets survive parsing; decode them back.
+ const codeText = decodeAngleBracketEntities(String(children));
return match ? (
<SyntaxHighlighter
{...restProps}
@@ -110,11 +114,11 @@ const MarkdownCode = (props: any) => {
language={match[1]}
wrapLongLines
>
- {String(children).replace(/\n$/, '')}
+ {codeText.replace(/\n$/, '')}
</SyntaxHighlighter>
) : (
<code {...restProps} className={classNames(className, 'text-wrap')}>
- {children}
+ {codeText}
</code>
);
};
diff --git a/web/src/components/next-markdown-content/index.tsx b/web/src/components/next-markdown-content/index.tsx
index 5d713ab12e..23a43ffae1 100644
--- a/web/src/components/next-markdown-content/index.tsx
+++ b/web/src/components/next-markdown-content/index.tsx
@@ -42,6 +42,7 @@ import 'katex/dist/katex.min.css'; // `rehype-katex` does not import the CSS for
import {
buildCitationDisplayMap,
currentReg,
+ decodeAngleBracketEntities,
escapeUnmatchedAngleBrackets,
parseCitationIndex,
preprocessLaTeX,
@@ -475,6 +476,10 @@ function MarkdownContent({
const { children, className, ...rest } = props;
const restProps = omit(rest, 'node');
const match = /language-(\w+)/.exec(className || '');
+ // Code content is literal for react-markdown, so the &lt;/&gt;
+ // entities restored by unescapeAngleBrackets survive parsing;
+ // decode them back.
+ const codeText = decodeAngleBracketEntities(String(children));
return match ? (
<SyntaxHighlighter
{...restProps}
@@ -482,14 +487,14 @@ function MarkdownContent({
language={match[1]}
wrapLongLines
>
- {String(children).replace(/\n$/, '')}
+ {codeText.replace(/\n$/, '')}
</SyntaxHighlighter>
) : (
<code
{...restProps}
className={classNames(className, 'text-wrap')}
>
- {children}
+ {codeText}
</code>
);
},
diff --git a/web/src/pages/next-search/markdown-content/index.tsx b/web/src/pages/next-search/markdown-content/index.tsx
index 95298b0fb7..7c58ef95ca 100644
--- a/web/src/pages/next-search/markdown-content/index.tsx
+++ b/web/src/pages/next-search/markdown-content/index.tsx
@@ -34,6 +34,7 @@ import 'katex/dist/katex.min.css'; // `rehype-katex` does not import the CSS for
import {
currentReg,
+ decodeAngleBracketEntities,
escapeUnmatchedAngleBrackets,
parseCitationIndex,
preprocessLaTeX,
@@ -294,6 +295,10 @@ const MarkdownContent = ({
const { children, className, ...rest } = props;
const restProps = omit(rest, 'node');
const match = /language-(\w+)/.exec(className || '');
+ // Code content is literal for react-markdown, so the &lt;/&gt;
+ // entities restored by unescapeAngleBrackets survive parsing;
+ // decode them back.
+ const codeText = decodeAngleBracketEntities(String(children));
return match ? (
<SyntaxHighlighter
{...restProps}
@@ -301,14 +306,14 @@ const MarkdownContent = ({
language={match[1]}
wrapLongLines
>
- {String(children).replace(/\n$/, '')}
+ {codeText.replace(/\n$/, '')}
</SyntaxHighlighter>
) : (
<code
{...restProps}
className={classNames(className, 'text-wrap')}
>
- {children}
+ {codeText}
</code>
);
},
diff --git a/web/src/utils/__tests__/chat.test.ts b/web/src/utils/__tests__/chat.test.ts
index ff4aa6aacb..928a4e566a 100644
--- a/web/src/utils/__tests__/chat.test.ts
+++ b/web/src/utils/__tests__/chat.test.ts
@@ -1,7 +1,10 @@
import {
+ decodeAngleBracketEntities,
+ escapeUnmatchedAngleBrackets,
mergeAnswerChunk,
preprocessLaTeX,
replaceThinkToSection,
+ unescapeAngleBrackets,
} from '../chat';
describe('mergeAnswerChunk', () => {
@@ -108,6 +111,34 @@ describe('preprocessLaTeX', () => {
});
});
+describe('decodeAngleBracketEntities', () => {
+ it('decodes the entities restored by unescapeAngleBrackets', () => {
+ expect(decodeAngleBracketEntities('a &lt; b &gt; c')).toBe('a < b > c');
+ });
+
+ it('round-trips raw angle brackets through the escape helpers', () => {
+ // Code renderers rely on this: unescapeAngleBrackets restores entities
+ // (so prose parsing keeps <, > as text), and code content is literal,
+ // so the entities must be decoded back at render time.
+ const original = 'if (a > b) return a - b < c;';
+ const escaped = unescapeAngleBrackets(
+ escapeUnmatchedAngleBrackets(original),
+ );
+ expect(escaped).toBe('if (a &gt; b) return a - b &lt; c;');
+ expect(decodeAngleBracketEntities(escaped)).toBe(original);
+ });
+
+ it('does not touch &amp; or double-encoded entities', () => {
+ expect(decodeAngleBracketEntities('a &amp; b &amp;gt; c')).toBe(
+ 'a &amp; b &amp;gt; c',
+ );
+ });
+
+ it.each(['', 'no entities here'])('passes through %j unchanged', (input) => {
+ expect(decodeAngleBracketEntities(input)).toBe(input);
+ });
+});
+
describe('replaceThinkToSection', () => {
it('drops an empty think section instead of rendering a bare strip', () => {
expect(replaceThinkToSection('<think></think>Here is the answer.')).toBe(
diff --git a/web/src/utils/chat.ts b/web/src/utils/chat.ts
index b16563da62..ccf0dbbcb9 100644
--- a/web/src/utils/chat.ts
+++ b/web/src/utils/chat.ts
@@ -220,6 +220,17 @@ export function unescapeAngleBrackets(content: string): string {
.replace(new RegExp(GT_MARKER, 'g'), '&gt;');
}
+/**
+ * Decode the &lt;/&gt; entities restored by unescapeAngleBrackets.
+ * react-markdown decodes character references in prose, but code blocks and
+ * inline code are literal, so the entities would render as-is there. Code
+ * renderers call this to show the original < and > the model emitted.
+ */
+export function decodeAngleBracketEntities(content: string): string {
+ if (!content) return content;
+ return content.replace(/&lt;/g, '<').replace(/&gt;/g, '>');
+}
+
export function setInitialChatVariableEnabledFieldValue(
field: ChatVariableEnabledField,
) {
@@ -245,11 +256,7 @@ export function setChatVariableEnabledFieldValuePage() {
const oldReg = /(#{2}[0-9\u0660-\u0669\u06F0-\u06F9]+\${2})/g;
export const currentReg = citationMarkerReg;
-export {
- buildCitationDisplayMap,
- normalizeCitationDigits,
- parseCitationIndex,
-};
+export { buildCitationDisplayMap, normalizeCitationDigits, parseCitationIndex };
// To be compatible with the old index matching mode
export const replaceTextByOldReg = (text: string) => {