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, '
{
+ 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",