diff --git a/packages/session-ui/src/components/markdown-stream.test.ts b/packages/session-ui/src/components/markdown-stream.test.ts index ebba8fe1355..8146b2016ee 100644 --- a/packages/session-ui/src/components/markdown-stream.test.ts +++ b/packages/session-ui/src/components/markdown-stream.test.ts @@ -17,6 +17,20 @@ describe("markdown stream", () => { expect(done.blocks.every((block) => block.mode === "full")).toBe(true) }) + test("sends a closed math fence to the Markdown parser and keeps a streaming one as code", () => { + const open = "Formula:\n\n```latex\nx^2" + expect(project(undefined, open, true).blocks.at(-1)).toMatchObject({ mode: "code", language: "latex" }) + + const text = open + "\n```\n\nAfter" + const closed = project(undefined, text, true) + expect(closed.blocks.map((block) => [block.raw, block.mode])).toContainEqual(["```latex\nx^2\n```\n\n", "full"]) + expect(closed.blocks.find((block) => block.raw.startsWith("```latex"))?.src).toStartWith("```latex\nx^2\n```") + + const done = project(closed, text, false) + expect(done.blocks.some((block) => block.mode === "code")).toBe(false) + expect(stream("```ts\nconst x = 1\n```\n", true)[0]).toMatchObject({ mode: "code", language: "ts" }) + }) + test("ignores > and /> inside quoted HTML attributes when grouping blocks", () => { const closed = project(undefined, '
\n
\n\nSettled\n\nTail', true) expect(closed.blocks.map((block) => [block.raw, block.mode])).toEqual([ diff --git a/packages/session-ui/src/components/markdown-stream.ts b/packages/session-ui/src/components/markdown-stream.ts index 84d0c1d1287..de9ace6d79e 100644 --- a/packages/session-ui/src/components/markdown-stream.ts +++ b/packages/session-ui/src/components/markdown-stream.ts @@ -1,4 +1,5 @@ import { createMarkdownStream, type MarkdownBlock, type PatchmarkStream } from "@opencode/patchmark" +import { isMathFence } from "@opencode/ui/context/marked-parser" import { completedProjection } from "./markdown-projection" export type Block = { @@ -35,7 +36,10 @@ function toBlock(block: MarkdownBlock): Block { if (cached) return cached - if (block.kind.type === "code" || block.kind.type === "mermaid") { + // A closed math fence goes to the Markdown parser, which draws it as display math; while it streams it stays code. + const math = block.kind.type === "code" && block.kind.complete && isMathFence(block.kind.language) + + if (!math && (block.kind.type === "code" || block.kind.type === "mermaid")) { const item: Block = { raw: block.raw, src: block.kind.source, @@ -53,7 +57,7 @@ function toBlock(block: MarkdownBlock): Block { return item } - const mode = block.status === "settled" ? "full" : "live" + const mode = math || block.status === "settled" ? "full" : "live" const defs = block.defs ? `\n\n${block.defs}` : "" const base = mode === "live" ? (block.liveRaw ?? block.raw) : block.raw diff --git a/packages/ui/src/context/marked-parser.test.ts b/packages/ui/src/context/marked-parser.test.ts index c129c152ac3..bf90a135b88 100644 --- a/packages/ui/src/context/marked-parser.test.ts +++ b/packages/ui/src/context/marked-parser.test.ts @@ -100,6 +100,26 @@ test("highlights fenced code with the app theme variables", async () => { expect(html).toContain('const ') }) +test.each(["latex", "math", "LaTeX", "latex title=\"Score\""])("renders a %s fence as display math", async (lang) => { + const html = await parser.parse( + `\`\`\`${lang}\nS_{\\text{adj}} = \\min\\!\\left(10,\\; S \\cdot \\left(\\frac{T_0}{T}\\right)^{\\alpha}\\right)\n\`\`\`\n`, + ) + + expect(html).toStartWith('') + expect(html).not.toContain(" { + expect(await parser.parse("- Score:\n\n ```latex\n x^2\n ```\n")).toContain('') +}) + +test.each(["\\frac{1}{", "\\notacommand{x}", ""])("keeps a math fence KaTeX cannot parse as code: %p", async (source) => { + const html = await parser.parse(`\`\`\`latex\n${source}\n\`\`\`\n`) + + expect(html).toContain('') + expect(html).not.toContain("katex") +}) + test("keeps the fence language for code without a lexer", async () => { const html = await parser.parse("```mermaid\ngraph TD; A-->B\n```\n") diff --git a/packages/ui/src/context/marked-parser.tsx b/packages/ui/src/context/marked-parser.tsx index 06d9b47909f..de4e0795c90 100644 --- a/packages/ui/src/context/marked-parser.tsx +++ b/packages/ui/src/context/marked-parser.tsx @@ -5,7 +5,7 @@ import { createMarkdownBase } from "./marked-base" import { OpenCodeTheme } from "./highlight-theme" export function createMarkdownParser() { - return createMarkdownBase().use(markdownMath, markdownCode) + return createMarkdownBase().use(markdownCode, markdownMath) } const decoder = new TextDecoder() @@ -41,7 +41,26 @@ function renderMath(token: Tokens.Generic) { return katex.renderToString(token.text, { throwOnError: false, displayMode: token.displayMode }) } +const mathFenceLanguages = new Set(["latex", "math"]) + +/** Whether a fence's info string names display math, as ```` ```latex ```` and ```` ```math ```` do in the TUI. */ +export function isMathFence(lang: string | undefined) { + return mathFenceLanguages.has(lang?.match(/^\S*/)?.[0].toLowerCase() ?? "") +} + export const markdownMath: MarkedExtension = { + renderer: { + // A math fence KaTeX cannot parse, such as one cut off mid-formula, stays highlighted code. + code(token) { + if (!isMathFence(token.lang) || !token.text.trim()) return false + + try { + return katex.renderToString(token.text, { throwOnError: true, displayMode: true }) + "\n" + } catch { + return false + } + }, + }, extensions: [ { name: "inlineDollarKatex",