fix(webui): stop repairing completed Markdown

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-30 15:50:27 +08:00
1 parent fc3effe39d
commit 450a85fe0e
6 files changed
+48 -7

No files matched your search

+1
View File
@@ -28,6 +28,7 @@
"remark-breaks": "^4.0.0",
"remark-gfm": "^4.0.0",
"remark-math": "^6.0.0",
"remend": "1.3.0",
"streamdown": "2.5.0",
"tailwind-merge": "^2.6.0",
},
+1
View File
@@ -36,6 +36,7 @@
"remark-breaks": "^4.0.0",
"remark-gfm": "^4.0.0",
"remark-math": "^6.0.0",
"remend": "1.3.0",
"streamdown": "2.5.0",
"tailwind-merge": "^2.6.0"
},
+5 -2
View File
@@ -25,12 +25,14 @@ const MemoizedMarkdownRenderer = memo(function MemoizedMarkdownRenderer({
className,
highlightCode,
streaming,
preserveStreamingLayout,
onOpenFilePreview,
}: {
source: string;
className?: string;
highlightCode: boolean;
streaming: boolean;
preserveStreamingLayout: boolean;
onOpenFilePreview?: (path: string) => void;
}) {
return (
@@ -38,6 +40,7 @@ const MemoizedMarkdownRenderer = memo(function MemoizedMarkdownRenderer({
className={className}
highlightCode={highlightCode}
streaming={streaming}
preserveStreamingLayout={preserveStreamingLayout}
onOpenFilePreview={onOpenFilePreview}
>
{source}
@@ -81,7 +84,6 @@ export function MarkdownText({
const renderedSource = children;
const renderPhase = streaming ? "streaming" : "complete";
const highlightCode = !streaming;
const renderWithStreamingLayout = streaming || preserveStreamingLayout;
useEffect(() => {
if (streaming) void preloadMarkdownText();
@@ -106,7 +108,8 @@ export function MarkdownText({
source={renderedSource}
className={className}
highlightCode={highlightCode}
streaming={renderWithStreamingLayout}
streaming={streaming}
preserveStreamingLayout={preserveStreamingLayout}
onOpenFilePreview={onOpenFilePreview}
/>
</Suspense>
@@ -12,6 +12,7 @@ import remarkBreaks from "remark-breaks";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import { Streamdown, type Components, type StreamdownProps } from "streamdown";
import remend from "remend";
import { AttachmentTile } from "@/components/AttachmentTile";
import { CodeBlock } from "@/components/CodeBlock";
@@ -42,6 +43,7 @@ interface MarkdownTextRendererProps {
className?: string;
highlightCode?: boolean;
streaming?: boolean;
preserveStreamingLayout?: boolean;
onOpenFilePreview?: (path: string) => void;
}
@@ -530,6 +532,7 @@ export default function MarkdownTextRenderer({
className,
highlightCode = true,
streaming = false,
preserveStreamingLayout = false,
onOpenFilePreview,
}: MarkdownTextRendererProps) {
const { t } = useTranslation();
@@ -826,9 +829,8 @@ export default function MarkdownTextRenderer({
return (
<Streamdown
key={needsMath && mathPlugin ? "math" : "text"}
mode={streaming ? "streaming" : "static"}
parseIncompleteMarkdown
remend={REMEND_OPTIONS}
mode={streaming || preserveStreamingLayout ? "streaming" : "static"}
parseIncompleteMarkdown={false}
isAnimating={false}
animated={false}
linkSafety={DIRECT_LINKS}
@@ -851,7 +853,8 @@ export default function MarkdownTextRenderer({
className,
)}
>
{children}
{/* Streamdown 2.5 ignores repair-option changes in its memo comparator. */}
{streaming ? remend(children, REMEND_OPTIONS) : children}
</Streamdown>
);
}
@@ -516,6 +516,30 @@ describe("MarkdownTextRenderer", () => {
expect(container.querySelector("[data-nanobot-stream-unit]")).not.toBeInTheDocument();
});
it("stops repairing completed markdown without replacing the streaming layout", () => {
const source = "The old snip_history() / _legal_history_tail() path was removed.\n\nA real maintenance cost.";
const { container, rerender } = render(
<MarkdownTextRenderer streaming preserveStreamingLayout>{source}</MarkdownTextRenderer>,
);
const firstParagraph = container.querySelector("p");
expect(container.querySelector("p:last-child")?.textContent).toBe("A real maintenance cost._");
rerender(<MarkdownTextRenderer preserveStreamingLayout>{source}</MarkdownTextRenderer>);
expect(container.querySelector("p:last-child")?.textContent).toBe("A real maintenance cost.");
expect(container.querySelector("p")).toBe(firstParagraph);
});
it.each(["A literal trailing underscore_", "**unfinished emphasis", "_legal_history_tail()"])(
"preserves completed source syntax: %s",
(source) => {
const { container } = render(
<MarkdownTextRenderer preserveStreamingLayout>{source}</MarkdownTextRenderer>,
);
expect(container.textContent).toBe(source);
},
);
it("repairs incomplete streaming markdown without exposing syntax fragments", () => {
const { container, rerender } = render(
<MarkdownTextRenderer streaming>{"**partial answer"}</MarkdownTextRenderer>,
+10 -1
View File
@@ -13,10 +13,12 @@ vi.mock("@/components/MarkdownTextRenderer", () => ({
children,
highlightCode,
streaming,
preserveStreamingLayout,
}: {
children: string;
highlightCode?: boolean;
streaming?: boolean;
preserveStreamingLayout?: boolean;
}) {
useEffect(() => {
rendererMountSpy();
@@ -29,7 +31,8 @@ vi.mock("@/components/MarkdownTextRenderer", () => ({
<div
data-testid="markdown-renderer"
data-highlight-code={String(highlightCode)}
data-streaming-layout={String(streaming)}
data-streaming-layout={String(streaming || preserveStreamingLayout)}
data-streaming={String(streaming)}
>
{children}
</div>
@@ -108,6 +111,7 @@ describe("MarkdownText", () => {
});
it("can complete without replacing the streaming renderer layout", async () => {
rendererMountSpy.mockClear();
const source = "A layout-stable answer";
const { rerender } = render(
<MarkdownText streaming preserveStreamingLayout>{source}</MarkdownText>,
@@ -122,8 +126,13 @@ describe("MarkdownText", () => {
"true",
);
expect(screen.getByTestId("markdown-renderer")).toHaveAttribute("data-streaming", "true");
rerender(<MarkdownText preserveStreamingLayout>{source}</MarkdownText>);
expect(screen.getByTestId("markdown-renderer")).toHaveAttribute("data-streaming", "false");
expect(rendererMountSpy).toHaveBeenCalledTimes(1);
expect(screen.getByTestId("markdown-renderer")).toHaveAttribute(
"data-streaming-layout",
"true",