Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(web): repair malformed strong-emphasis spacing in assistant output (#786)

p421098cfbd49bad09e
diff --git a/web/components/common/AssistantResponse.tsx b/web/components/common/AssistantResponse.tsx
index a2f943023..fa5c5e3de 100644
--- a/web/components/common/AssistantResponse.tsx
+++ b/web/components/common/AssistantResponse.tsx
@@ -6,6 +6,7 @@ import MarkdownRenderer from "@/components/common/MarkdownRenderer";
import ModelThinkingCard from "@/components/common/ModelThinkingCard";
import {
hasVisibleMarkdownContent,
+ repairMalformedStrongEmphasis,
stripArtifactAnnotations,
} from "@/lib/markdown-display";
import { parseModelThinkingSegments } from "@/lib/think-segments";
@@ -71,15 +72,16 @@ function AssistantResponseImpl({
/>
);
}
+ const repairedContent = repairMalformedStrongEmphasis(segment.content);
- if (!hasVisibleMarkdownContent(segment.content)) {
+ if (!hasVisibleMarkdownContent(repairedContent)) {
return <Fragment key={`text-${index}`} />;
}
return (
<MarkdownRenderer
key={`text-${index}`}
- content={segment.content}
+ content={repairedContent}
variant="prose"
className="text-[var(--foreground)]"
/>
diff --git a/web/lib/markdown-display.ts b/web/lib/markdown-display.ts
index 3d72ca627..0d451cc42 100644
--- a/web/lib/markdown-display.ts
+++ b/web/lib/markdown-display.ts
@@ -144,6 +144,14 @@ const INLINE_CODE_SPAN_REGEX = /`[^`\n]*`/g;
// inline math ($x = [1, 5, 9]$) is protected from citation linkification.
const MATH_SPAN_REGEX =
/\\\[[\s\S]*?\\\]|\\\([\s\S]*?\\\)|\$\$[\s\S]*?\$\$|\$(?!\s)(?:\\.|[^$\n])*?(?<!\s)\$/g;
+// ``**Label: **value`` — a label whose closing marker has whitespace just
+// inside it, which CommonMark does not read as strong emphasis, so the raw
+// asterisks stay on screen. Deliberately narrow: the capture must start at a
+// non-space and end at a colon, because this rewrite has no notion of which
+// two delimiters the author meant to pair (see repairStrongEmphasisLine).
+const MALFORMED_STRONG_EMPHASIS_REGEX =
+ /(?<!\S)\*\*(?=\S)([^*\n]*?[::])[ \t]+\*\*(?=\S)/g;
+const INDENTED_CODE_LINE_REGEX = /^(?: {4}|\t)/;
const PROTECTED_SPAN_REGEX = /```[\s\S]*?```|`[^`\n]*`/g;
const PROTECTED_PLACEHOLDER_REGEX = /\u0000PROTECTED_(\d+)\u0000/g;
const HTML_ATTR_VALUE = /(?:"[^"]*"|'[^']*'|[^\s"'=<>`]+)/.source;
@@ -554,6 +562,51 @@ function maskProtectedSpans(
};
}
+export function repairMalformedStrongEmphasis(content: string): string {
+ if (!content.includes("**")) return content;
+
+ const fenced = maskProtectedSpans(
+ content,
+ FENCED_CODE_BLOCK_REGEX,
+ "STRONG_FENCED_CODE",
+ );
+ const math = maskProtectedSpans(
+ fenced.masked,
+ MATH_SPAN_REGEX,
+ "STRONG_MATH",
+ );
+ const inline = maskProtectedSpans(
+ math.masked,
+ INLINE_CODE_SPAN_REGEX,
+ "STRONG_INLINE_CODE",
+ );
+
+ const repaired = inline.masked
+ .split("\n")
+ .map(repairStrongEmphasisLine)
+ .join("\n");
+
+ return fenced.restore(math.restore(inline.restore(repaired)));
+}
+
+/**
+ * Repair one line, or leave it exactly as it was.
+ *
+ * The regex pairs an opening ``**`` with the next one on the line, which is
+ * only the author's intent when every marker on that line is paired off. With
+ * an odd count at least one is literal or unclosed, and rewriting then breaks
+ * emphasis the renderer gets right today — ``In Markdown, use ** to make text
+ * **bold**.`` would lose its bold, and ``**Note: **Important**`` would end up
+ * with a stray ``**``. Bailing out costs nothing: the line renders exactly as
+ * it does on a build without this repair.
+ */
+function repairStrongEmphasisLine(line: string): string {
+ // Indented code blocks are displayed verbatim and are not masked above.
+ if (INDENTED_CODE_LINE_REGEX.test(line)) return line;
+ if ((line.split("**").length - 1) % 2 !== 0) return line;
+ return line.replace(MALFORMED_STRONG_EMPHASIS_REGEX, "**$1** ");
+}
+
function linkifyCitationsOutsideCode(content: string): string {
const fenced = maskProtectedSpans(
content,
diff --git a/web/tests/markdown-display.test.ts b/web/tests/markdown-display.test.ts
index d0161482b..7a93220e9 100644
--- a/web/tests/markdown-display.test.ts
+++ b/web/tests/markdown-display.test.ts
@@ -5,9 +5,81 @@ import {
hasVisibleMarkdownContent,
markdownUrlTransform,
normalizeMarkdownForDisplay,
+ repairMalformedStrongEmphasis,
safeDecodeURIComponent,
} from "../lib/markdown-display";
+test("repairMalformedStrongEmphasis moves label whitespace outside the closing marker", () => {
+ assert.equal(
+ repairMalformedStrongEmphasis("**發布日期: **2026 年 7 月 30 日"),
+ "**發布日期:** 2026 年 7 月 30 日",
+ );
+});
+
+test("repairMalformedStrongEmphasis preserves valid and incomplete Markdown", () => {
+ const inputs = [
+ "**發布日期:** 2026 年 7 月 30 日",
+ "Use **bold text** normally.",
+ "**發布日期: 2026 年 7 月 30 日",
+ "**發布日期: **",
+ ];
+
+ for (const input of inputs) {
+ assert.equal(repairMalformedStrongEmphasis(input), input);
+ }
+});
+
+test("repairMalformedStrongEmphasis leaves code and math spans untouched", () => {
+ const input = [
+ "`**label: **value`",
+ "",
+ "```md",
+ "**label: **value",
+ "```",
+ "",
+ "$\\text{**label: **value}$",
+ "",
+ "\\[",
+ "**label: **value",
+ "\\]",
+ ].join("\n");
+
+ assert.equal(repairMalformedStrongEmphasis(input), input);
+});
+
+test("repairMalformedStrongEmphasis leaves lines whose ** markers don't pair off", () => {
+ // Each of these renders correctly today; pairing the first two ``**`` would
+ // break emphasis the renderer already gets right.
+ const inputs = [
+ // Prose that mentions ** literally, followed by a real bold span.
+ "In Markdown, use ** to make text **bold**.",
+ // A malformed label immediately followed by a legitimate bold span:
+ // repairing the label would leave a stray ** behind.
+ "**Note: **Important**",
+ // Nested strong emphasis, which CommonMark renders as all-bold.
+ "**重點 **必讀** 內容**",
+ ];
+
+ for (const input of inputs) {
+ assert.equal(repairMalformedStrongEmphasis(input), input);
+ }
+});
+
+test("repairMalformedStrongEmphasis leaves indented code blocks verbatim", () => {
+ const input = "Example:\n\n **label: **value";
+
+ assert.equal(repairMalformedStrongEmphasis(input), input);
+});
+
+test("repairMalformedStrongEmphasis repairs multiple occurrences idempotently", () => {
+ const input = "**Date: **2026 and **Source: **Official";
+ const expected = "**Date:** 2026 and **Source:** Official";
+ const repaired = repairMalformedStrongEmphasis(input);
+
+ assert.equal(repaired, expected);
+ assert.equal(repairMalformedStrongEmphasis(repaired), expected);
+});
+
test("normalizeMarkdownForDisplay removes empty details blocks", () => {
const input = "Before\n\n<details><summary></summary></details>\n\nAfter";
assert.equal(normalizeMarkdownForDisplay(input), "Before\n\nAfter");