mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
feat(app): render latex and math code fences as display math (#64380)
Co-authored-by: Brendan <brendan@anoma.ly>
This commit is contained in:
committed by
GitHub
Unverified
parent
c658753fdb
commit
e78605ac39
@@ -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, '<div data-tip="a > <span>">\n</div>\n\nSettled\n\nTail', true)
|
||||
expect(closed.blocks.map((block) => [block.raw, block.mode])).toEqual([
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -100,6 +100,26 @@ test("highlights fenced code with the app theme variables", async () => {
|
||||
expect(html).toContain('<span style="color:var(--syntax-keyword)">const </span>')
|
||||
})
|
||||
|
||||
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('<span class="katex-display">')
|
||||
expect(html).not.toContain("<pre")
|
||||
})
|
||||
|
||||
test("renders a math fence inside a list item", async () => {
|
||||
expect(await parser.parse("- Score:\n\n ```latex\n x^2\n ```\n")).toContain('<span class="katex-display">')
|
||||
})
|
||||
|
||||
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('<code class="language-latex">')
|
||||
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")
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user