mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 00:51:17 +08:00
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.
89 lines
3.0 KiB
TypeScript
89 lines
3.0 KiB
TypeScript
"use client";
|
|
|
|
import dynamic from "next/dynamic";
|
|
import { hasMarkdownMath } from "@/lib/latex";
|
|
import SimpleMarkdownRenderer from "./SimpleMarkdownRenderer";
|
|
import type { MarkdownRendererProps } from "./markdown-renderer-types";
|
|
|
|
export type { MarkdownRendererProps } from "./markdown-renderer-types";
|
|
|
|
const RichMarkdownRenderer = dynamic(() => import("./RichMarkdownRenderer"), {
|
|
ssr: false,
|
|
});
|
|
|
|
function detectCodeContent(content: string): boolean {
|
|
// Match any opening triple-backtick, even before the language identifier
|
|
// arrives. Otherwise streaming flips Simple→Rich the moment the language
|
|
// tag lands a few tokens after the fence.
|
|
return /```/.test(content);
|
|
}
|
|
|
|
function detectMermaidContent(content: string): boolean {
|
|
// editor.md style ```flow / ```seq / ```sequence fences are converted to
|
|
// mermaid by processMarkdownContent, so they need to enable the mermaid path
|
|
// as well. Otherwise the converted blocks fall through to the code renderer.
|
|
return /```(?:mermaid|flow|seq|sequence)\b/i.test(content);
|
|
}
|
|
|
|
function detectHtmlContent(content: string): boolean {
|
|
return /<\/?[A-Za-z][\w:-]*(\s|>)/.test(content);
|
|
}
|
|
|
|
export default function MarkdownRenderer({
|
|
content,
|
|
className = "",
|
|
variant = "default",
|
|
enableMath,
|
|
enableCode,
|
|
enableMermaid,
|
|
enableImages,
|
|
allowHtml,
|
|
trackSourceLines,
|
|
}: MarkdownRendererProps) {
|
|
const resolvedEnableMath = enableMath ?? hasMarkdownMath(content);
|
|
const resolvedEnableCode = enableCode ?? detectCodeContent(content);
|
|
const resolvedEnableMermaid = enableMermaid ?? detectMermaidContent(content);
|
|
const resolvedAllowHtml = allowHtml ?? detectHtmlContent(content);
|
|
// Detection above is intentionally monotonic — once any of the rich
|
|
// triggers fire, more tokens never un-fire them. Combined with the
|
|
// append-only nature of streaming content this gives us a stable
|
|
// Simple→Rich one-way transition (the Rich subtree mounts once and
|
|
// stays). No additional lock state is needed.
|
|
//
|
|
// `trace` is not excluded. It used to be, which pinned every trace bubble to
|
|
// the Simple renderer and left `RichMarkdownRenderer`'s whole `trace` branch
|
|
// unreachable — so a tutoring round that reasoned in formulas printed raw
|
|
// `$y'' + 2y' + 5y = 0$` at the reader. The trigger is the content, not the
|
|
// surface: a trace bubble with no math still takes the cheap path.
|
|
const shouldUseRich =
|
|
trackSourceLines ||
|
|
resolvedEnableMath ||
|
|
resolvedEnableCode ||
|
|
resolvedEnableMermaid ||
|
|
resolvedAllowHtml;
|
|
|
|
if (!shouldUseRich) {
|
|
return (
|
|
<SimpleMarkdownRenderer
|
|
content={content}
|
|
className={className}
|
|
variant={variant}
|
|
/>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<RichMarkdownRenderer
|
|
content={content}
|
|
className={className}
|
|
variant={variant}
|
|
enableMath={resolvedEnableMath}
|
|
enableCode={resolvedEnableCode}
|
|
enableMermaid={resolvedEnableMermaid}
|
|
enableImages={enableImages}
|
|
allowHtml={resolvedAllowHtml}
|
|
trackSourceLines={trackSourceLines}
|
|
/>
|
|
);
|
|
}
|