@zyro-sec/latex 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/layout.ts +673 -673
- package/src/limits.ts +40 -40
- package/src/markdown.ts +116 -116
- package/src/parser.ts +598 -598
- package/src/plugin.ts +14 -14
- package/src/render.ts +11 -11
- package/src/symbols.ts +315 -315
- package/src/types.ts +97 -97
- package/tsconfig.json +10 -10
- package/src/layout.test.ts +0 -87
- package/src/markdown.test.ts +0 -267
- package/src/parser-render.test.ts +0 -23
- package/src/parser.test.ts +0 -298
- package/src/render.test.ts +0 -87
- package/src/root.test.ts +0 -69
package/src/types.ts
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
export type MathVariant = "normal" | "bold" | "italic" | "sans" | "monospace" | "double-struck" | "script" | "fraktur"
|
|
2
|
-
|
|
3
|
-
export type MathNode =
|
|
4
|
-
| { type: "row"; body: MathNode[] }
|
|
5
|
-
| { type: "symbol"; value: string; role?: SymbolRole }
|
|
6
|
-
| { type: "text"; value: string }
|
|
7
|
-
| { type: "space"; width: number }
|
|
8
|
-
| {
|
|
9
|
-
type: "fraction"
|
|
10
|
-
numerator: MathNode
|
|
11
|
-
denominator: MathNode
|
|
12
|
-
bar: boolean
|
|
13
|
-
numeratorAlign?: "left" | "right"
|
|
14
|
-
}
|
|
15
|
-
| { type: "root"; body: MathNode; index?: MathNode }
|
|
16
|
-
| { type: "scripts"; base: MathNode; superscript?: MathNode; subscript?: MathNode }
|
|
17
|
-
| { type: "delimited"; left: string; body: MathNode; right: string }
|
|
18
|
-
| { type: "matrix"; rows: MathNode[][]; environment: MatrixEnvironment; columns?: string }
|
|
19
|
-
| { type: "brace"; body: MathNode; position: "over" | "under" }
|
|
20
|
-
| { type: "accent"; accent: AccentKind; body: MathNode }
|
|
21
|
-
| { type: "variant"; variant: MathVariant; body: MathNode }
|
|
22
|
-
| { type: "operator"; value: string; limits: boolean }
|
|
23
|
-
| { type: "overunder"; base: MathNode; over?: MathNode; under?: MathNode }
|
|
24
|
-
| { type: "color"; color: string; body: MathNode }
|
|
25
|
-
|
|
26
|
-
export type SymbolRole = "ordinary" | "binary" | "relation" | "operator" | "punctuation" | "opening" | "closing"
|
|
27
|
-
|
|
28
|
-
export type MatrixEnvironment =
|
|
29
|
-
| "matrix"
|
|
30
|
-
| "pmatrix"
|
|
31
|
-
| "bmatrix"
|
|
32
|
-
| "Bmatrix"
|
|
33
|
-
| "vmatrix"
|
|
34
|
-
| "Vmatrix"
|
|
35
|
-
| "cases"
|
|
36
|
-
| "aligned"
|
|
37
|
-
| "align"
|
|
38
|
-
| "gathered"
|
|
39
|
-
| "gather"
|
|
40
|
-
| "smallmatrix"
|
|
41
|
-
| "array"
|
|
42
|
-
|
|
43
|
-
export type AccentKind = "hat" | "widehat" | "bar" | "overline" | "underline" | "vec" | "tilde" | "dot" | "ddot"
|
|
44
|
-
|
|
45
|
-
export interface ParseOptions {
|
|
46
|
-
macros?: Readonly<Record<string, string>>
|
|
47
|
-
maxExpand?: number
|
|
48
|
-
/**
|
|
49
|
-
* Maximum accepted input length. This guards interactive and AI-generated
|
|
50
|
-
* formulas against accidentally exhausting the terminal process.
|
|
51
|
-
*/
|
|
52
|
-
maxSourceLength?: number
|
|
53
|
-
/**
|
|
54
|
-
* Maximum length after user-macro expansion. Defaults to
|
|
55
|
-
* `maxSourceLength`.
|
|
56
|
-
*/
|
|
57
|
-
maxExpandedLength?: number
|
|
58
|
-
/** Maximum structural nesting depth. */
|
|
59
|
-
maxDepth?: number
|
|
60
|
-
strict?: boolean
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export class LatexParseError extends Error {
|
|
64
|
-
public readonly position: number
|
|
65
|
-
|
|
66
|
-
constructor(message: string, position: number) {
|
|
67
|
-
super(`${message} at offset ${position}`)
|
|
68
|
-
this.name = "LatexParseError"
|
|
69
|
-
this.position = position
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export interface MathStyle {
|
|
74
|
-
color?: string
|
|
75
|
-
bold?: boolean
|
|
76
|
-
italic?: boolean
|
|
77
|
-
dim?: boolean
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export interface MathCell {
|
|
81
|
-
char: string
|
|
82
|
-
style?: MathStyle
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export interface MathLayout {
|
|
86
|
-
readonly width: number
|
|
87
|
-
readonly height: number
|
|
88
|
-
readonly baseline: number
|
|
89
|
-
readonly cells: ReadonlyArray<ReadonlyArray<MathCell | undefined>>
|
|
90
|
-
toString(): string
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export interface RenderLatexOptions extends ParseOptions {
|
|
94
|
-
displayMode?: boolean
|
|
95
|
-
compactScripts?: boolean
|
|
96
|
-
color?: string
|
|
97
|
-
}
|
|
1
|
+
export type MathVariant = "normal" | "bold" | "italic" | "sans" | "monospace" | "double-struck" | "script" | "fraktur"
|
|
2
|
+
|
|
3
|
+
export type MathNode =
|
|
4
|
+
| { type: "row"; body: MathNode[] }
|
|
5
|
+
| { type: "symbol"; value: string; role?: SymbolRole }
|
|
6
|
+
| { type: "text"; value: string }
|
|
7
|
+
| { type: "space"; width: number }
|
|
8
|
+
| {
|
|
9
|
+
type: "fraction"
|
|
10
|
+
numerator: MathNode
|
|
11
|
+
denominator: MathNode
|
|
12
|
+
bar: boolean
|
|
13
|
+
numeratorAlign?: "left" | "right"
|
|
14
|
+
}
|
|
15
|
+
| { type: "root"; body: MathNode; index?: MathNode }
|
|
16
|
+
| { type: "scripts"; base: MathNode; superscript?: MathNode; subscript?: MathNode }
|
|
17
|
+
| { type: "delimited"; left: string; body: MathNode; right: string }
|
|
18
|
+
| { type: "matrix"; rows: MathNode[][]; environment: MatrixEnvironment; columns?: string }
|
|
19
|
+
| { type: "brace"; body: MathNode; position: "over" | "under" }
|
|
20
|
+
| { type: "accent"; accent: AccentKind; body: MathNode }
|
|
21
|
+
| { type: "variant"; variant: MathVariant; body: MathNode }
|
|
22
|
+
| { type: "operator"; value: string; limits: boolean }
|
|
23
|
+
| { type: "overunder"; base: MathNode; over?: MathNode; under?: MathNode }
|
|
24
|
+
| { type: "color"; color: string; body: MathNode }
|
|
25
|
+
|
|
26
|
+
export type SymbolRole = "ordinary" | "binary" | "relation" | "operator" | "punctuation" | "opening" | "closing"
|
|
27
|
+
|
|
28
|
+
export type MatrixEnvironment =
|
|
29
|
+
| "matrix"
|
|
30
|
+
| "pmatrix"
|
|
31
|
+
| "bmatrix"
|
|
32
|
+
| "Bmatrix"
|
|
33
|
+
| "vmatrix"
|
|
34
|
+
| "Vmatrix"
|
|
35
|
+
| "cases"
|
|
36
|
+
| "aligned"
|
|
37
|
+
| "align"
|
|
38
|
+
| "gathered"
|
|
39
|
+
| "gather"
|
|
40
|
+
| "smallmatrix"
|
|
41
|
+
| "array"
|
|
42
|
+
|
|
43
|
+
export type AccentKind = "hat" | "widehat" | "bar" | "overline" | "underline" | "vec" | "tilde" | "dot" | "ddot"
|
|
44
|
+
|
|
45
|
+
export interface ParseOptions {
|
|
46
|
+
macros?: Readonly<Record<string, string>>
|
|
47
|
+
maxExpand?: number
|
|
48
|
+
/**
|
|
49
|
+
* Maximum accepted input length. This guards interactive and AI-generated
|
|
50
|
+
* formulas against accidentally exhausting the terminal process.
|
|
51
|
+
*/
|
|
52
|
+
maxSourceLength?: number
|
|
53
|
+
/**
|
|
54
|
+
* Maximum length after user-macro expansion. Defaults to
|
|
55
|
+
* `maxSourceLength`.
|
|
56
|
+
*/
|
|
57
|
+
maxExpandedLength?: number
|
|
58
|
+
/** Maximum structural nesting depth. */
|
|
59
|
+
maxDepth?: number
|
|
60
|
+
strict?: boolean
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export class LatexParseError extends Error {
|
|
64
|
+
public readonly position: number
|
|
65
|
+
|
|
66
|
+
constructor(message: string, position: number) {
|
|
67
|
+
super(`${message} at offset ${position}`)
|
|
68
|
+
this.name = "LatexParseError"
|
|
69
|
+
this.position = position
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface MathStyle {
|
|
74
|
+
color?: string
|
|
75
|
+
bold?: boolean
|
|
76
|
+
italic?: boolean
|
|
77
|
+
dim?: boolean
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface MathCell {
|
|
81
|
+
char: string
|
|
82
|
+
style?: MathStyle
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface MathLayout {
|
|
86
|
+
readonly width: number
|
|
87
|
+
readonly height: number
|
|
88
|
+
readonly baseline: number
|
|
89
|
+
readonly cells: ReadonlyArray<ReadonlyArray<MathCell | undefined>>
|
|
90
|
+
toString(): string
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface RenderLatexOptions extends ParseOptions {
|
|
94
|
+
displayMode?: boolean
|
|
95
|
+
compactScripts?: boolean
|
|
96
|
+
color?: string
|
|
97
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://json.schemastore.org/tsconfig",
|
|
3
|
-
"extends": "@tsconfig/bun/tsconfig.json",
|
|
4
|
-
"compilerOptions": {
|
|
5
|
-
"types": ["bun"],
|
|
6
|
-
"noUnusedLocals": true,
|
|
7
|
-
"lib": ["ESNext", "DOM", "DOM.Iterable"],
|
|
8
|
-
"noUncheckedIndexedAccess": false
|
|
9
|
-
}
|
|
10
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/tsconfig",
|
|
3
|
+
"extends": "@tsconfig/bun/tsconfig.json",
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"types": ["bun"],
|
|
6
|
+
"noUnusedLocals": true,
|
|
7
|
+
"lib": ["ESNext", "DOM", "DOM.Iterable"],
|
|
8
|
+
"noUncheckedIndexedAccess": false
|
|
9
|
+
}
|
|
10
|
+
}
|
package/src/layout.test.ts
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test"
|
|
2
|
-
import { layoutMath } from "./layout"
|
|
3
|
-
import { renderLatexToString } from "./render"
|
|
4
|
-
|
|
5
|
-
const text = (value: string) => ({ type: "text" as const, value })
|
|
6
|
-
|
|
7
|
-
describe("structured math layout", () => {
|
|
8
|
-
test.each([
|
|
9
|
-
String.raw`\sqrt{x}`,
|
|
10
|
-
String.raw`\begin{pmatrix}a&b\\c&d\end{pmatrix}`,
|
|
11
|
-
String.raw`\underbrace{abcd}`,
|
|
12
|
-
String.raw`\overbrace{abcd}`,
|
|
13
|
-
String.raw`\sum`,
|
|
14
|
-
])("empty scripts do not change geometry: %s", (source) => {
|
|
15
|
-
for (const scripts of ["^{}", "_{}", "^{}_{}"]) {
|
|
16
|
-
expect(renderLatexToString(source + scripts)).toBe(renderLatexToString(source))
|
|
17
|
-
}
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
test("centers annotations over even-width brace junctions", () => {
|
|
21
|
-
expect(renderLatexToString(String.raw`\overbrace{abcd}^{n}`)).toBe([" n", "╭┴─╮", "abcd"].join("\n"))
|
|
22
|
-
expect(renderLatexToString(String.raw`\underbrace{abcd}_{n}`)).toBe(["abcd", "╰┬─╯", " n"].join("\n"))
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
test("raises powers above tall matrix delimiters", () => {
|
|
26
|
-
expect(renderLatexToString(String.raw`\begin{pmatrix}a&b\\c&d\end{pmatrix}^2`)).toBe(
|
|
27
|
-
[" 2", "⎛a b⎞", "⎜ ⎟", "⎝c d⎠"].join("\n"),
|
|
28
|
-
)
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
test("keeps piecewise values left-aligned", () => {
|
|
32
|
-
expect(renderLatexToString(String.raw`\begin{cases}x & x>0\\x^2+1 & x\le0\end{cases}`)).toBe(
|
|
33
|
-
["⎧x x > 0", "⎨", "⎩x² + 1 x ≤ 0"].join("\n"),
|
|
34
|
-
)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
test("honors array column alignment and continuous separators", () => {
|
|
38
|
-
expect(
|
|
39
|
-
layoutMath({
|
|
40
|
-
type: "matrix",
|
|
41
|
-
environment: "array",
|
|
42
|
-
columns: "l|r",
|
|
43
|
-
rows: [
|
|
44
|
-
[text("a"), text("wide")],
|
|
45
|
-
[text("long"), text("b")],
|
|
46
|
-
],
|
|
47
|
-
}).toString(),
|
|
48
|
-
).toBe(["a │ wide", " │", "long │ b"].join("\n"))
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
test("preserves edge rules and double array separators", () => {
|
|
52
|
-
expect(
|
|
53
|
-
layoutMath({
|
|
54
|
-
type: "matrix",
|
|
55
|
-
environment: "array",
|
|
56
|
-
columns: "|l||r|",
|
|
57
|
-
rows: [
|
|
58
|
-
[text("a"), text("b")],
|
|
59
|
-
[text("long"), text("c")],
|
|
60
|
-
],
|
|
61
|
-
}).toString(),
|
|
62
|
-
).toBe(["│ a ││ b │", "│ ││ │", "│ long ││ c │"].join("\n"))
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
test.each(["left", "right"] as const)("aligns continued-fraction numerators to the %s", (numeratorAlign) => {
|
|
66
|
-
const layout = layoutMath({
|
|
67
|
-
type: "fraction",
|
|
68
|
-
numerator: text("1"),
|
|
69
|
-
denominator: text("12345"),
|
|
70
|
-
bar: true,
|
|
71
|
-
numeratorAlign,
|
|
72
|
-
})
|
|
73
|
-
expect(layout.toString()).toBe([numeratorAlign === "left" ? " 1" : " 1", "───────", " 12345"].join("\n"))
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
test.each(["over", "under"] as const)("stretches %s braces and places annotations outside them", (position) => {
|
|
77
|
-
const layout = layoutMath({
|
|
78
|
-
type: "scripts",
|
|
79
|
-
base: { type: "brace", body: text("a + b + c"), position },
|
|
80
|
-
...(position === "over" ? { superscript: text("n") } : { subscript: text("n") }),
|
|
81
|
-
})
|
|
82
|
-
expect(layout.toString()).toBe(
|
|
83
|
-
(position === "over" ? [" n", "╭───┴───╮", "a + b + c"] : ["a + b + c", "╰───┬───╯", " n"]).join("\n"),
|
|
84
|
-
)
|
|
85
|
-
expect(layout.baseline).toBe(position === "over" ? 2 : 0)
|
|
86
|
-
})
|
|
87
|
-
})
|
package/src/markdown.test.ts
DELETED
|
@@ -1,267 +0,0 @@
|
|
|
1
|
-
import { afterEach, expect, test } from "bun:test"
|
|
2
|
-
import {
|
|
3
|
-
CodeRenderable,
|
|
4
|
-
MarkdownRenderable,
|
|
5
|
-
RGBA,
|
|
6
|
-
ScrollBoxRenderable,
|
|
7
|
-
SyntaxStyle,
|
|
8
|
-
TextAttributes,
|
|
9
|
-
TextRenderable,
|
|
10
|
-
createMarkdownCodeBlockRenderer,
|
|
11
|
-
} from "@opentui/core"
|
|
12
|
-
import { createTestRenderer } from "@opentui/core/testing"
|
|
13
|
-
import { renderLatex } from "./render"
|
|
14
|
-
import { createLatexCodeBlockRenderer } from "./markdown"
|
|
15
|
-
|
|
16
|
-
const renderers: {
|
|
17
|
-
renderer: Awaited<ReturnType<typeof createTestRenderer>>["renderer"]
|
|
18
|
-
syntaxStyle: SyntaxStyle
|
|
19
|
-
}[] = []
|
|
20
|
-
|
|
21
|
-
afterEach(async () => {
|
|
22
|
-
await Promise.all(
|
|
23
|
-
renderers.splice(0).map(async (output) => {
|
|
24
|
-
output.renderer.destroy()
|
|
25
|
-
await output.renderer.closed
|
|
26
|
-
output.syntaxStyle.destroy()
|
|
27
|
-
}),
|
|
28
|
-
)
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
async function setup(content: string, width = 80) {
|
|
32
|
-
const output = await createTestRenderer({
|
|
33
|
-
width,
|
|
34
|
-
height: 24,
|
|
35
|
-
remote: true,
|
|
36
|
-
})
|
|
37
|
-
const syntaxStyle = SyntaxStyle.fromStyles({ default: { fg: "#ffffff" } }, output.renderer.nativeScene)
|
|
38
|
-
renderers.push({ renderer: output.renderer, syntaxStyle })
|
|
39
|
-
const palette = { text: "#abcdef", subdued: "#667788" }
|
|
40
|
-
const render = createLatexCodeBlockRenderer(output.renderer, () => palette)
|
|
41
|
-
const markdown = new MarkdownRenderable(output.renderer, {
|
|
42
|
-
content,
|
|
43
|
-
syntaxStyle,
|
|
44
|
-
streaming: true,
|
|
45
|
-
renderBlock: createMarkdownCodeBlockRenderer({ latex: render, math: render }),
|
|
46
|
-
})
|
|
47
|
-
output.renderer.root.add(markdown)
|
|
48
|
-
await output.renderOnce()
|
|
49
|
-
return { ...output, markdown, palette }
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
test.each(["latex", "math", "tex", "LATEX title=example"])("renders a %s fence", async (language) => {
|
|
53
|
-
const output = await setup(`\`\`\`${language}\n\\frac{1}{2}\n\`\`\``)
|
|
54
|
-
const formula = output.markdown.getChildren()[0]?.getChildren()[0]
|
|
55
|
-
expect(formula).toBeInstanceOf(TextRenderable)
|
|
56
|
-
if (!(formula instanceof TextRenderable)) throw new Error("Expected a formula")
|
|
57
|
-
expect(formula.height).toBe(3)
|
|
58
|
-
expect(formula.chunks.find((chunk) => chunk.text === "1")?.fg?.equals(RGBA.fromHex("#abcdef"))).toBe(true)
|
|
59
|
-
expect(output.captureCharFrame()).toContain("1")
|
|
60
|
-
expect(output.captureCharFrame()).toContain("2")
|
|
61
|
-
expect(output.captureCharFrame()).not.toContain("\\frac")
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
test.each([
|
|
65
|
-
String.raw`\frac{1}{`,
|
|
66
|
-
String.raw`\unsupported{x}`,
|
|
67
|
-
String.raw`\cfrac[x]{1}{2}`,
|
|
68
|
-
String.raw`\left\unknown x\right)`,
|
|
69
|
-
String.raw`\begin{array}{p{2cm}}x\end{array}`,
|
|
70
|
-
String.raw`\documentclass{article}
|
|
71
|
-
\begin{document}
|
|
72
|
-
Hello
|
|
73
|
-
\end{document}`,
|
|
74
|
-
])("preserves invalid or unsupported math as source: %s", async (source) => {
|
|
75
|
-
const output = await setup(`\`\`\`latex\n${source}\n\`\`\``)
|
|
76
|
-
const block = output.markdown.getChildren()[0]
|
|
77
|
-
expect(block).toBeInstanceOf(CodeRenderable)
|
|
78
|
-
if (!(block instanceof CodeRenderable)) throw new Error("Expected source fallback")
|
|
79
|
-
expect(block.content).toBe(source)
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
test.each([
|
|
83
|
-
String.raw`\sqrt[\frac{1}{2}]{x}`,
|
|
84
|
-
String.raw`\left\|v\right\|`,
|
|
85
|
-
String.raw`\left(A\rightarrow B\right)`,
|
|
86
|
-
String.raw`\begin{aligned}a&=b+c\\&=d\end{aligned}`,
|
|
87
|
-
String.raw`\displaylines{x=1\\y=2}`,
|
|
88
|
-
String.raw`\cfrac[l]{1}{12345}`,
|
|
89
|
-
String.raw`\underbrace{a+b+c}_{n}`,
|
|
90
|
-
String.raw`\begin{array}{l|r}a&wide\\long&b\end{array}`,
|
|
91
|
-
])("renders structured math through the Markdown adapter: %s", async (source) => {
|
|
92
|
-
const output = await setup(`\`\`\`latex\n${source}\n\`\`\``)
|
|
93
|
-
expect(output.markdown.getChildren()[0]).toBeInstanceOf(ScrollBoxRenderable)
|
|
94
|
-
expect(output.markdown.getChildren()[0]?.getChildren()[0]).toBeInstanceOf(TextRenderable)
|
|
95
|
-
expect(output.captureCharFrame()).not.toContain("\\")
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
test("renders the next valid formula after an incomplete streaming prefix", async () => {
|
|
99
|
-
const output = await setup("```latex\n\\frac{1}{")
|
|
100
|
-
expect(output.markdown.getChildren()[0]).toBeInstanceOf(CodeRenderable)
|
|
101
|
-
|
|
102
|
-
output.markdown.content += "2}"
|
|
103
|
-
await output.renderOnce()
|
|
104
|
-
expect(output.markdown.getChildren()[0]?.getChildren()[0]).toBeInstanceOf(TextRenderable)
|
|
105
|
-
expect(output.captureCharFrame()).not.toContain("\\frac")
|
|
106
|
-
|
|
107
|
-
output.markdown.content += "\n```"
|
|
108
|
-
output.markdown.streaming = false
|
|
109
|
-
await output.renderOnce()
|
|
110
|
-
expect(output.markdown.getChildren()[0]?.getChildren()[0]).toBeInstanceOf(TextRenderable)
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
test("renders the final formula when the last text update is applied before completion", async () => {
|
|
114
|
-
const output = await setup("```latex\n\\frac{1}{")
|
|
115
|
-
output.markdown.content += "2}\n```"
|
|
116
|
-
output.markdown.streaming = false
|
|
117
|
-
await output.renderOnce()
|
|
118
|
-
expect(output.markdown.getChildren().filter((child) => child instanceof ScrollBoxRenderable).length).toBe(1)
|
|
119
|
-
expect(output.captureCharFrame()).not.toContain("\\frac")
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
test("retains the last valid Unicode formula while the next fraction is incomplete", async () => {
|
|
123
|
-
const output = await setup("```latex\n\\frac{a_1+b_1}{c_1+d_1}")
|
|
124
|
-
const previous = output.captureCharFrame()
|
|
125
|
-
output.markdown.content += "+\\frac{a_"
|
|
126
|
-
await output.renderOnce()
|
|
127
|
-
expect(output.markdown.getChildren()[0]).toBeInstanceOf(ScrollBoxRenderable)
|
|
128
|
-
expect(output.captureCharFrame()).toBe(previous)
|
|
129
|
-
|
|
130
|
-
output.markdown.content += "2+b_2}{c_2+d_2}"
|
|
131
|
-
await output.renderOnce()
|
|
132
|
-
expect(output.captureCharFrame()).not.toBe(previous)
|
|
133
|
-
expect(output.captureCharFrame()).not.toContain("\\frac")
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
test.each(["close", "stop"])("discards an incomplete preview when the stream ends: %s", async (end) => {
|
|
137
|
-
const output = await setup("```latex\nx^2")
|
|
138
|
-
output.markdown.content += " + \\frac{1}{"
|
|
139
|
-
await output.renderOnce()
|
|
140
|
-
expect(output.markdown.getChildren()[0]).toBeInstanceOf(ScrollBoxRenderable)
|
|
141
|
-
|
|
142
|
-
// A closing fence counts once its line ends.
|
|
143
|
-
if (end === "close") output.markdown.content += "\n```\n"
|
|
144
|
-
if (end === "stop") output.markdown.streaming = false
|
|
145
|
-
await output.renderOnce()
|
|
146
|
-
expect(output.markdown.getChildren()[0]).toBeInstanceOf(CodeRenderable)
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
test("a closed fence that stops parsing shows its source after a rendered formula", async () => {
|
|
150
|
-
const output = await setup("```math\n\\frac{")
|
|
151
|
-
output.markdown.content += "1}{2}\n"
|
|
152
|
-
await output.renderOnce()
|
|
153
|
-
expect(output.markdown.getChildren()[0]).toBeInstanceOf(ScrollBoxRenderable)
|
|
154
|
-
output.markdown.content += "\\badunknown\n```\n"
|
|
155
|
-
output.markdown.streaming = false
|
|
156
|
-
await output.renderOnce()
|
|
157
|
-
const fallback = output.markdown.getChildren()[0]
|
|
158
|
-
expect(fallback).toBeInstanceOf(CodeRenderable)
|
|
159
|
-
expect(fallback!.isDestroyed).toBe(false)
|
|
160
|
-
expect(output.captureCharFrame()).toContain("\\badunknown")
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
test("does not reuse another fence's preview or keep a removed fence's preview", async () => {
|
|
164
|
-
const output = await setup("```latex\nx^2\n```\n\n```latex\n\\frac{1}{")
|
|
165
|
-
expect(output.markdown.getChildren()[1]).toBeInstanceOf(CodeRenderable)
|
|
166
|
-
|
|
167
|
-
output.markdown.content = ""
|
|
168
|
-
await output.renderOnce()
|
|
169
|
-
output.markdown.content = "```latex\nx^2 + \\frac{1}{"
|
|
170
|
-
await output.renderOnce()
|
|
171
|
-
expect(output.markdown.getChildren()[0]).toBeInstanceOf(CodeRenderable)
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
test("does not leave a stale formula when a stream ends with invalid math", async () => {
|
|
175
|
-
const output = await setup("```latex\nx^2")
|
|
176
|
-
expect(output.markdown.getChildren()[0]?.getChildren()[0]).toBeInstanceOf(TextRenderable)
|
|
177
|
-
|
|
178
|
-
output.markdown.content += " + \\unsupported{x}\n```"
|
|
179
|
-
output.markdown.streaming = false
|
|
180
|
-
await output.renderOnce()
|
|
181
|
-
expect(output.markdown.getChildren()[0]).toBeInstanceOf(CodeRenderable)
|
|
182
|
-
})
|
|
183
|
-
|
|
184
|
-
test("keeps a matrix and surrounding Markdown intact in a narrow terminal", async () => {
|
|
185
|
-
const output = await setup("Before\n\n```math\n\\begin{pmatrix}a & b \\\\ c & d\\end{pmatrix}\n```\n\nAfter", 32)
|
|
186
|
-
await output.renderOnce()
|
|
187
|
-
const frame = output.captureCharFrame()
|
|
188
|
-
expect(frame).toContain("Before")
|
|
189
|
-
expect(frame).toContain("a b")
|
|
190
|
-
expect(frame).toContain("c d")
|
|
191
|
-
expect(frame).toContain("After")
|
|
192
|
-
expect(frame).not.toContain("pmatrix")
|
|
193
|
-
})
|
|
194
|
-
|
|
195
|
-
test("leaves ordinary code fences alone", async () => {
|
|
196
|
-
const output = await setup("```typescript\nconst x = 2\n```")
|
|
197
|
-
expect(output.markdown.getChildren()[0]).toBeInstanceOf(CodeRenderable)
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
test("allows wide formulas to scroll horizontally without wrapping", async () => {
|
|
201
|
-
const output = await setup(
|
|
202
|
-
"```latex\n\\text{Start a very long formula with enough content to overflow Finish}\n```",
|
|
203
|
-
24,
|
|
204
|
-
)
|
|
205
|
-
const viewport = output.markdown.getChildren()[0]
|
|
206
|
-
expect(viewport).toBeInstanceOf(ScrollBoxRenderable)
|
|
207
|
-
if (!(viewport instanceof ScrollBoxRenderable)) throw new Error("Expected a horizontal viewport")
|
|
208
|
-
expect(output.captureCharFrame()).toContain("Start")
|
|
209
|
-
expect(output.captureCharFrame()).not.toContain("Finish")
|
|
210
|
-
expect(viewport.height).toBe(1)
|
|
211
|
-
|
|
212
|
-
await output.mockMouse.scroll(2, 1, "right")
|
|
213
|
-
await output.renderOnce()
|
|
214
|
-
expect(viewport.scrollLeft).toBeGreaterThan(0)
|
|
215
|
-
|
|
216
|
-
viewport.scrollLeft = viewport.scrollWidth
|
|
217
|
-
await output.renderOnce()
|
|
218
|
-
expect(output.captureCharFrame()).toContain("Finish")
|
|
219
|
-
expect(output.captureCharFrame()).not.toContain("Start")
|
|
220
|
-
})
|
|
221
|
-
|
|
222
|
-
test("subdues structure and emphasizes relations using the theme", async () => {
|
|
223
|
-
const output = await setup("```latex\nx=\\sqrt{\\frac{1}{2}}\n```")
|
|
224
|
-
const formula = output.markdown.getChildren()[0]?.getChildren()[0]
|
|
225
|
-
if (!(formula instanceof TextRenderable)) throw new Error("Expected Unicode math")
|
|
226
|
-
for (const mark of ["\u2500", "\u2502", "\u256d", "\u256f", "\u2570"]) {
|
|
227
|
-
expect(formula.chunks.find((chunk) => chunk.text === mark)?.fg?.equals(RGBA.fromHex(output.palette.subdued))).toBe(
|
|
228
|
-
true,
|
|
229
|
-
)
|
|
230
|
-
}
|
|
231
|
-
expect(formula.chunks.find((chunk) => chunk.text === "x")?.fg?.equals(RGBA.fromHex(output.palette.text))).toBe(true)
|
|
232
|
-
expect(formula.chunks.find((chunk) => chunk.text === "=")?.attributes).toBe(TextAttributes.BOLD)
|
|
233
|
-
|
|
234
|
-
output.palette.text = "#123456"
|
|
235
|
-
output.palette.subdued = "#789abc"
|
|
236
|
-
output.markdown.refreshStyles()
|
|
237
|
-
await output.renderOnce()
|
|
238
|
-
const updated = output.markdown.getChildren()[0]?.getChildren()[0]
|
|
239
|
-
if (!(updated instanceof TextRenderable)) throw new Error("Expected Unicode math")
|
|
240
|
-
expect(updated.chunks.find((chunk) => chunk.text === "x")?.fg?.equals(RGBA.fromHex(output.palette.text))).toBe(true)
|
|
241
|
-
for (const mark of ["\u2500", "\u2502", "\u256d", "\u256f", "\u2570"]) {
|
|
242
|
-
expect(updated.chunks.find((chunk) => chunk.text === mark)?.fg?.equals(RGBA.fromHex(output.palette.subdued))).toBe(
|
|
243
|
-
true,
|
|
244
|
-
)
|
|
245
|
-
}
|
|
246
|
-
})
|
|
247
|
-
|
|
248
|
-
test.each([String.raw`\text{${"\u4e2d\u6587"}}=x`, String.raw`\frac{\text{${"\u4e2d\u6587"}}}{abcd}=x`])(
|
|
249
|
-
"preserves wide-character alignment: %s",
|
|
250
|
-
async (source) => {
|
|
251
|
-
const layout = renderLatex(source)
|
|
252
|
-
const output = await setup(`\`\`\`latex\n${source}\n\`\`\``, layout.width)
|
|
253
|
-
const viewport = output.markdown.getChildren()[0]
|
|
254
|
-
if (!(viewport instanceof ScrollBoxRenderable)) throw new Error("Expected math viewport")
|
|
255
|
-
const formula = viewport.getChildren()[0]
|
|
256
|
-
if (!(formula instanceof TextRenderable)) throw new Error("Expected Unicode math")
|
|
257
|
-
expect(
|
|
258
|
-
formula.chunks
|
|
259
|
-
.map((chunk) => chunk.text)
|
|
260
|
-
.join("")
|
|
261
|
-
.split("\n")
|
|
262
|
-
.map((line) => line.trimEnd())
|
|
263
|
-
.join("\n"),
|
|
264
|
-
).toBe(layout.toString())
|
|
265
|
-
expect(viewport.scrollWidth).toBe(layout.width)
|
|
266
|
-
},
|
|
267
|
-
)
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test"
|
|
2
|
-
import { renderLatexToString } from "./render"
|
|
3
|
-
|
|
4
|
-
describe("parser rendering regressions", () => {
|
|
5
|
-
test.each([
|
|
6
|
-
[String.raw`\|v\|`, "║v║"],
|
|
7
|
-
[String.raw`\left\|v\right\|`, "║v║"],
|
|
8
|
-
[String.raw`\left|v\right|`, "│v│"],
|
|
9
|
-
[String.raw`\left(A\rightarrow B\right)`, "(A → B)"],
|
|
10
|
-
[String.raw`\left\lbrace x\right\rbrace`, "{x}"],
|
|
11
|
-
[String.raw`\operatorname{arg\,max} x`, "arg max x"],
|
|
12
|
-
[String.raw`\textrm{if }x`, "if x"],
|
|
13
|
-
[String.raw`\displaylines{x=1\\y=2}`, "x = 1\n\ny = 2"],
|
|
14
|
-
])("renders supported syntax without leaking or losing tokens: %s", (source, expected) => {
|
|
15
|
-
expect(renderLatexToString(source, { strict: true })).toBe(expected)
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
test("renders empty aligned cells like explicitly empty groups", () => {
|
|
19
|
-
expect(renderLatexToString(String.raw`\begin{aligned}&=x\\&=y\end{aligned}`, { strict: true })).toBe(
|
|
20
|
-
renderLatexToString(String.raw`\begin{aligned}{}&=x\\{}&=y\end{aligned}`, { strict: true }),
|
|
21
|
-
)
|
|
22
|
-
})
|
|
23
|
-
})
|