Files
DeepTutor/web/components/common/InlineMarkdown.tsx
Bingxi Zhao (Frank) 70650c02d1 fix(web): render the tutor's formulas on the surfaces a learner reads
A maths or physics tutoring turn reaches the learner with its LaTeX
intact: a Bellman question shows `$G_t = R_{t+1} + \gamma G_{t+1}$`, a
derivation shows `$\Delta = 4 - 20 = -16 < 0$`, and a bolded cue shows
its asterisks. Three separate surfaces, one cause each.

`MarkdownRenderer` refused the rich renderer whenever the variant was
`trace`, so no trace bubble could ever reach KaTeX — and
`RichMarkdownRenderer`'s entire `isTrace` branch, written for exactly
this, was unreachable code. Rich is chosen by what the content contains,
not by which surface shows it; a trace bubble with no math still takes
the cheap path, which is what that exclusion was really protecting.

The mastery question card and the ask_user card interpolated plain
strings. They are also short, and they sit inside `<button>` and
one-line rows, so the block renderer is the wrong tool: a `<p>` is not
valid there and brings block spacing with it. `InlineMarkdown` emits
phrasing content only and loads KaTeX at all only when the string has
something to render, so an ordinary question costs nothing. It now
renders the mastery stem, its options and its verdict explanation, and
the ask_user intro, stem, option labels and option descriptions.

Escaped-unicode decoding moved inside it, so callers no longer choose
between decoding a string and rendering it.
2026-09-10 22:51:27 +08:00

43 lines
1.5 KiB
TypeScript

"use client";
import dynamic from "next/dynamic";
import { hasMarkdownMath } from "@/lib/latex";
import { decodeEscapedUnicodeForDisplay } from "@/lib/markdown-display";
const RichInlineMarkdown = dynamic(() => import("./RichInlineMarkdown"), {
ssr: false,
});
// Emphasis, inline code and links are the rest of what a tutor writes into a
// question stem or an option. Matching them here (not just math) is what keeps
// `**听懂**` from reaching the learner with its asterisks attached.
const INLINE_MARKUP_RE = /(\*\*|__|`|~~|\[[^\]]+\]\()/;
/**
* A short string a model wrote, rendered as the inline markdown it is.
*
* Question stems, answer options and grade explanations were printed as plain
* text, so a physics or maths question showed the learner raw `$V^\pi$` and a
* bolded cue showed its asterisks. They are also *short* and sit inside
* buttons and one-line rows, so they cannot use the block renderer: this one
* emits phrasing content only, and loads KaTeX at all only when the string
* actually contains something to render.
*/
export default function InlineMarkdown({
content,
className,
}: {
content: string;
className?: string;
}) {
const text = decodeEscapedUnicodeForDisplay(content ?? "");
const rich = hasMarkdownMath(text) || INLINE_MARKUP_RE.test(text);
if (!rich) {
return className ? <span className={className}>{text}</span> : <>{text}</>;
}
const rendered = <RichInlineMarkdown content={text} />;
return className ? <span className={className}>{rendered}</span> : rendered;
}