Files
DeepTutor/web/components/common/InlineMarkdown.tsx
lytanjie 284e06145d fix(web): render GFM tables in learner card stems (#2879)
Learner-card stems go through InlineMarkdown, which only switches to the
rich renderer for math, emphasis, code and links; a stem whose only markup
was a table reached the learner as raw pipes. Detect GFM tables and route
those stems through RichInlineMarkdown, and add markdown-table.ts (table
detection plus a blank line after each table so a trailing sentence is not
swallowed as another row).

Tests: learner-card-table.spec.tsx, markdown-table.test.ts.

Co-authored-by: Claude Code <noreply@anthropic.com>
2026-11-29 14:53:07 +08:00

49 lines
1.7 KiB
TypeScript

"use client";
import dynamic from "next/dynamic";
import { hasMarkdownMath } from "@/lib/latex";
import { decodeEscapedUnicodeForDisplay } from "@/lib/markdown-display";
import { hasMarkdownTable } from "@/lib/markdown-table";
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. A table is
// the one block construct a stem carries often enough to need the rich path
// too — its delimiter row is the reliable tell.
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 =
hasMarkdownTable(text) ||
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;
}