Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions packages/latex/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "@opencode-ai/latex",
"version": "0.0.0",
"private": true,
"type": "module",
"exports": {
"./markdown": "./src/markdown.ts",
"./plugin": "./src/plugin.ts"
},
"scripts": {
"test": "bun test --timeout 30000 --only-failures",
"typecheck": "tsgo --noEmit"
},
"dependencies": {
"@opencode-ai/plugin": "workspace:*",
"@opentui/core": "catalog:",
"string-width": "catalog:"
},
"devDependencies": {
"@tsconfig/bun": "catalog:",
"@types/bun": "catalog:",
"@typescript/native-preview": "catalog:"
}
}
87 changes: 87 additions & 0 deletions packages/latex/src/layout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { describe, expect, test } from "bun:test"
import { layoutMath } from "./layout"
import { renderLatexToString } from "./render"

const text = (value: string) => ({ type: "text" as const, value })

describe("structured math layout", () => {
test.each([
String.raw`\sqrt{x}`,
String.raw`\begin{pmatrix}a&b\\c&d\end{pmatrix}`,
String.raw`\underbrace{abcd}`,
String.raw`\overbrace{abcd}`,
String.raw`\sum`,
])("empty scripts do not change geometry: %s", (source) => {
for (const scripts of ["^{}", "_{}", "^{}_{}"]) {
expect(renderLatexToString(source + scripts)).toBe(renderLatexToString(source))
}
})

test("centers annotations over even-width brace junctions", () => {
expect(renderLatexToString(String.raw`\overbrace{abcd}^{n}`)).toBe([" n", "╭┴─╮", "abcd"].join("\n"))
expect(renderLatexToString(String.raw`\underbrace{abcd}_{n}`)).toBe(["abcd", "╰┬─╯", " n"].join("\n"))
})

test("raises powers above tall matrix delimiters", () => {
expect(renderLatexToString(String.raw`\begin{pmatrix}a&b\\c&d\end{pmatrix}^2`)).toBe(
[" 2", "⎛a b⎞", "⎜ ⎟", "⎝c d⎠"].join("\n"),
)
})

test("keeps piecewise values left-aligned", () => {
expect(renderLatexToString(String.raw`\begin{cases}x & x>0\\x^2+1 & x\le0\end{cases}`)).toBe(
["⎧x x > 0", "⎨", "⎩x² + 1 x ≤ 0"].join("\n"),
)
})

test("honors array column alignment and continuous separators", () => {
expect(
layoutMath({
type: "matrix",
environment: "array",
columns: "l|r",
rows: [
[text("a"), text("wide")],
[text("long"), text("b")],
],
}).toString(),
).toBe(["a │ wide", " │", "long │ b"].join("\n"))
})

test("preserves edge rules and double array separators", () => {
expect(
layoutMath({
type: "matrix",
environment: "array",
columns: "|l||r|",
rows: [
[text("a"), text("b")],
[text("long"), text("c")],
],
}).toString(),
).toBe(["│ a ││ b │", "│ ││ │", "│ long ││ c │"].join("\n"))
})

test.each(["left", "right"] as const)("aligns continued-fraction numerators to the %s", (numeratorAlign) => {
const layout = layoutMath({
type: "fraction",
numerator: text("1"),
denominator: text("12345"),
bar: true,
numeratorAlign,
})
expect(layout.toString()).toBe([numeratorAlign === "left" ? " 1" : " 1", "───────", " 12345"].join("\n"))
})

test.each(["over", "under"] as const)("stretches %s braces and places annotations outside them", (position) => {
const layout = layoutMath({
type: "scripts",
base: { type: "brace", body: text("a + b + c"), position },
...(position === "over" ? { superscript: text("n") } : { subscript: text("n") }),
})
expect(layout.toString()).toBe(
(position === "over" ? [" n", "╭───┴───╮", "a + b + c"] : ["a + b + c", "╰───┬───╯", " n"]).join("\n"),
)
expect(layout.baseline).toBe(position === "over" ? 2 : 0)
})
})
Loading
Loading