@zyro-sec/latex 0.0.0-stage → 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 +26 -4
- package/src/layout.ts +673 -0
- package/src/limits.ts +40 -0
- package/src/markdown.ts +116 -0
- package/src/parser.ts +598 -0
- package/src/plugin.ts +14 -0
- package/src/render.ts +11 -0
- package/src/symbols.ts +315 -0
- package/src/types.ts +97 -0
- package/tsconfig.json +10 -0
- package/README.md +0 -3
package/src/symbols.ts
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import type { SymbolRole } from "./types"
|
|
2
|
+
|
|
3
|
+
export interface SymbolDefinition {
|
|
4
|
+
value: string
|
|
5
|
+
role?: SymbolRole
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const ordinary: Record<string, string> = {
|
|
9
|
+
alpha: "α",
|
|
10
|
+
beta: "β",
|
|
11
|
+
gamma: "γ",
|
|
12
|
+
delta: "δ",
|
|
13
|
+
epsilon: "ε",
|
|
14
|
+
varepsilon: "ϵ",
|
|
15
|
+
zeta: "ζ",
|
|
16
|
+
eta: "η",
|
|
17
|
+
theta: "θ",
|
|
18
|
+
vartheta: "ϑ",
|
|
19
|
+
iota: "ι",
|
|
20
|
+
kappa: "κ",
|
|
21
|
+
lambda: "λ",
|
|
22
|
+
mu: "μ",
|
|
23
|
+
nu: "ν",
|
|
24
|
+
xi: "ξ",
|
|
25
|
+
omicron: "ο",
|
|
26
|
+
pi: "π",
|
|
27
|
+
varpi: "ϖ",
|
|
28
|
+
rho: "ρ",
|
|
29
|
+
varrho: "ϱ",
|
|
30
|
+
sigma: "σ",
|
|
31
|
+
varsigma: "ς",
|
|
32
|
+
tau: "τ",
|
|
33
|
+
upsilon: "υ",
|
|
34
|
+
phi: "ϕ",
|
|
35
|
+
varphi: "φ",
|
|
36
|
+
chi: "χ",
|
|
37
|
+
psi: "ψ",
|
|
38
|
+
omega: "ω",
|
|
39
|
+
Gamma: "Γ",
|
|
40
|
+
Delta: "Δ",
|
|
41
|
+
Theta: "Θ",
|
|
42
|
+
Lambda: "Λ",
|
|
43
|
+
Xi: "Ξ",
|
|
44
|
+
Pi: "Π",
|
|
45
|
+
Sigma: "Σ",
|
|
46
|
+
Upsilon: "Υ",
|
|
47
|
+
Phi: "Φ",
|
|
48
|
+
Psi: "Ψ",
|
|
49
|
+
Omega: "Ω",
|
|
50
|
+
infty: "∞",
|
|
51
|
+
ell: "ℓ",
|
|
52
|
+
hbar: "ℏ",
|
|
53
|
+
imath: "ı",
|
|
54
|
+
jmath: "ȷ",
|
|
55
|
+
Re: "ℜ",
|
|
56
|
+
Im: "ℑ",
|
|
57
|
+
aleph: "ℵ",
|
|
58
|
+
beth: "ℶ",
|
|
59
|
+
gimel: "ℷ",
|
|
60
|
+
daleth: "ℸ",
|
|
61
|
+
partial: "∂",
|
|
62
|
+
nabla: "∇",
|
|
63
|
+
angle: "∠",
|
|
64
|
+
measuredangle: "∡",
|
|
65
|
+
triangle: "△",
|
|
66
|
+
square: "□",
|
|
67
|
+
lozenge: "◊",
|
|
68
|
+
top: "⊤",
|
|
69
|
+
bot: "⊥",
|
|
70
|
+
emptyset: "∅",
|
|
71
|
+
varnothing: "∅",
|
|
72
|
+
forall: "∀",
|
|
73
|
+
exists: "∃",
|
|
74
|
+
nexists: "∄",
|
|
75
|
+
neg: "¬",
|
|
76
|
+
lnot: "¬",
|
|
77
|
+
prime: "′",
|
|
78
|
+
backprime: "‵",
|
|
79
|
+
clubsuit: "♣",
|
|
80
|
+
diamondsuit: "♢",
|
|
81
|
+
heartsuit: "♡",
|
|
82
|
+
spadesuit: "♠",
|
|
83
|
+
checkmark: "✓",
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const binary: Record<string, string> = {
|
|
87
|
+
pm: "±",
|
|
88
|
+
mp: "∓",
|
|
89
|
+
times: "×",
|
|
90
|
+
div: "÷",
|
|
91
|
+
cdot: "·",
|
|
92
|
+
ast: "∗",
|
|
93
|
+
star: "⋆",
|
|
94
|
+
circ: "∘",
|
|
95
|
+
bullet: "∙",
|
|
96
|
+
oplus: "⊕",
|
|
97
|
+
ominus: "⊖",
|
|
98
|
+
otimes: "⊗",
|
|
99
|
+
oslash: "⊘",
|
|
100
|
+
odot: "⊙",
|
|
101
|
+
cap: "∩",
|
|
102
|
+
cup: "∪",
|
|
103
|
+
uplus: "⊎",
|
|
104
|
+
sqcap: "⊓",
|
|
105
|
+
sqcup: "⊔",
|
|
106
|
+
vee: "∨",
|
|
107
|
+
lor: "∨",
|
|
108
|
+
wedge: "∧",
|
|
109
|
+
land: "∧",
|
|
110
|
+
setminus: "∖",
|
|
111
|
+
wr: "≀",
|
|
112
|
+
diamond: "⋄",
|
|
113
|
+
bigtriangleup: "△",
|
|
114
|
+
bigtriangledown: "▽",
|
|
115
|
+
triangleleft: "◁",
|
|
116
|
+
triangleright: "▷",
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const relation: Record<string, string> = {
|
|
120
|
+
equals: "=",
|
|
121
|
+
neq: "≠",
|
|
122
|
+
ne: "≠",
|
|
123
|
+
equiv: "≡",
|
|
124
|
+
approx: "≈",
|
|
125
|
+
sim: "∼",
|
|
126
|
+
simeq: "≃",
|
|
127
|
+
cong: "≅",
|
|
128
|
+
asymp: "≍",
|
|
129
|
+
propto: "∝",
|
|
130
|
+
lt: "<",
|
|
131
|
+
gt: ">",
|
|
132
|
+
le: "≤",
|
|
133
|
+
leq: "≤",
|
|
134
|
+
ge: "≥",
|
|
135
|
+
geq: "≥",
|
|
136
|
+
ll: "≪",
|
|
137
|
+
gg: "≫",
|
|
138
|
+
prec: "≺",
|
|
139
|
+
succ: "≻",
|
|
140
|
+
preceq: "⪯",
|
|
141
|
+
succeq: "⪰",
|
|
142
|
+
subset: "⊂",
|
|
143
|
+
supset: "⊃",
|
|
144
|
+
subseteq: "⊆",
|
|
145
|
+
supseteq: "⊇",
|
|
146
|
+
sqsubset: "⊏",
|
|
147
|
+
sqsupset: "⊐",
|
|
148
|
+
sqsubseteq: "⊑",
|
|
149
|
+
sqsupseteq: "⊒",
|
|
150
|
+
in: "∈",
|
|
151
|
+
ni: "∋",
|
|
152
|
+
notin: "∉",
|
|
153
|
+
owns: "∋",
|
|
154
|
+
vdash: "⊢",
|
|
155
|
+
dashv: "⊣",
|
|
156
|
+
models: "⊨",
|
|
157
|
+
mid: "∣",
|
|
158
|
+
parallel: "∥",
|
|
159
|
+
perp: "⊥",
|
|
160
|
+
smile: "⌣",
|
|
161
|
+
frown: "⌢",
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const arrows: Record<string, string> = {
|
|
165
|
+
leftarrow: "←",
|
|
166
|
+
gets: "←",
|
|
167
|
+
rightarrow: "→",
|
|
168
|
+
to: "→",
|
|
169
|
+
leftrightarrow: "↔",
|
|
170
|
+
Leftarrow: "⇐",
|
|
171
|
+
Rightarrow: "⇒",
|
|
172
|
+
Leftrightarrow: "⇔",
|
|
173
|
+
mapsto: "↦",
|
|
174
|
+
hookleftarrow: "↩",
|
|
175
|
+
hookrightarrow: "↪",
|
|
176
|
+
leftharpoonup: "↼",
|
|
177
|
+
leftharpoondown: "↽",
|
|
178
|
+
rightharpoonup: "⇀",
|
|
179
|
+
rightharpoondown: "⇁",
|
|
180
|
+
rightleftharpoons: "⇌",
|
|
181
|
+
longleftarrow: "⟵",
|
|
182
|
+
longrightarrow: "⟶",
|
|
183
|
+
longleftrightarrow: "⟷",
|
|
184
|
+
Longleftarrow: "⟸",
|
|
185
|
+
Longrightarrow: "⟹",
|
|
186
|
+
Longleftrightarrow: "⟺",
|
|
187
|
+
longmapsto: "⟼",
|
|
188
|
+
uparrow: "↑",
|
|
189
|
+
downarrow: "↓",
|
|
190
|
+
updownarrow: "↕",
|
|
191
|
+
Uparrow: "⇑",
|
|
192
|
+
Downarrow: "⇓",
|
|
193
|
+
Updownarrow: "⇕",
|
|
194
|
+
nearrow: "↗",
|
|
195
|
+
searrow: "↘",
|
|
196
|
+
swarrow: "↙",
|
|
197
|
+
nwarrow: "↖",
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const punctuation: Record<string, string> = {
|
|
201
|
+
cdots: "⋯",
|
|
202
|
+
ldots: "…",
|
|
203
|
+
dots: "…",
|
|
204
|
+
vdots: "⋮",
|
|
205
|
+
ddots: "⋱",
|
|
206
|
+
colon: ":",
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export const symbolTable: Readonly<Record<string, SymbolDefinition>> = {
|
|
210
|
+
...Object.fromEntries(Object.entries(ordinary).map(([name, value]) => [name, { value, role: "ordinary" as const }])),
|
|
211
|
+
...Object.fromEntries(Object.entries(binary).map(([name, value]) => [name, { value, role: "binary" as const }])),
|
|
212
|
+
...Object.fromEntries(Object.entries(relation).map(([name, value]) => [name, { value, role: "relation" as const }])),
|
|
213
|
+
...Object.fromEntries(Object.entries(arrows).map(([name, value]) => [name, { value, role: "relation" as const }])),
|
|
214
|
+
...Object.fromEntries(
|
|
215
|
+
Object.entries(punctuation).map(([name, value]) => [name, { value, role: "punctuation" as const }]),
|
|
216
|
+
),
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export const largeOperators: Readonly<Record<string, string>> = {
|
|
220
|
+
sum: "∑",
|
|
221
|
+
prod: "∏",
|
|
222
|
+
coprod: "∐",
|
|
223
|
+
int: "∫",
|
|
224
|
+
iint: "∬",
|
|
225
|
+
iiint: "∭",
|
|
226
|
+
oint: "∮",
|
|
227
|
+
bigcap: "⋂",
|
|
228
|
+
bigcup: "⋃",
|
|
229
|
+
bigvee: "⋁",
|
|
230
|
+
bigwedge: "⋀",
|
|
231
|
+
bigoplus: "⨁",
|
|
232
|
+
bigotimes: "⨂",
|
|
233
|
+
bigodot: "⨀",
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export const namedOperators = new Set([
|
|
237
|
+
"arccos",
|
|
238
|
+
"arcsin",
|
|
239
|
+
"arctan",
|
|
240
|
+
"arg",
|
|
241
|
+
"cos",
|
|
242
|
+
"cosh",
|
|
243
|
+
"cot",
|
|
244
|
+
"coth",
|
|
245
|
+
"csc",
|
|
246
|
+
"deg",
|
|
247
|
+
"det",
|
|
248
|
+
"dim",
|
|
249
|
+
"exp",
|
|
250
|
+
"gcd",
|
|
251
|
+
"hom",
|
|
252
|
+
"inf",
|
|
253
|
+
"ker",
|
|
254
|
+
"lg",
|
|
255
|
+
"lim",
|
|
256
|
+
"liminf",
|
|
257
|
+
"limsup",
|
|
258
|
+
"ln",
|
|
259
|
+
"log",
|
|
260
|
+
"max",
|
|
261
|
+
"min",
|
|
262
|
+
"mod",
|
|
263
|
+
"Pr",
|
|
264
|
+
"sec",
|
|
265
|
+
"sin",
|
|
266
|
+
"sinh",
|
|
267
|
+
"sup",
|
|
268
|
+
"tan",
|
|
269
|
+
"tanh",
|
|
270
|
+
])
|
|
271
|
+
|
|
272
|
+
export const delimiterTable: Readonly<Record<string, string>> = {
|
|
273
|
+
"(": "(",
|
|
274
|
+
")": ")",
|
|
275
|
+
"[": "[",
|
|
276
|
+
"]": "]",
|
|
277
|
+
"\\{": "{",
|
|
278
|
+
"\\}": "}",
|
|
279
|
+
"{": "{",
|
|
280
|
+
"}": "}",
|
|
281
|
+
"|": "│",
|
|
282
|
+
"\\|": "║",
|
|
283
|
+
vert: "│",
|
|
284
|
+
Vert: "║",
|
|
285
|
+
lvert: "│",
|
|
286
|
+
rvert: "│",
|
|
287
|
+
lVert: "║",
|
|
288
|
+
rVert: "║",
|
|
289
|
+
lbrace: "{",
|
|
290
|
+
rbrace: "}",
|
|
291
|
+
langle: "⟨",
|
|
292
|
+
rangle: "⟩",
|
|
293
|
+
lfloor: "⌊",
|
|
294
|
+
rfloor: "⌋",
|
|
295
|
+
lceil: "⌈",
|
|
296
|
+
rceil: "⌉",
|
|
297
|
+
".": "",
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export const spacingCommands: Readonly<Record<string, number>> = {
|
|
301
|
+
// "\ " is the LaTeX control space: a backslash followed by a literal space.
|
|
302
|
+
" ": 1,
|
|
303
|
+
// A backslash before a tab, a carriage return, or a newline is the same control space.
|
|
304
|
+
"\t": 1,
|
|
305
|
+
"\r": 1,
|
|
306
|
+
"\n": 1,
|
|
307
|
+
",": 0,
|
|
308
|
+
":": 1,
|
|
309
|
+
";": 1,
|
|
310
|
+
"!": 0,
|
|
311
|
+
quad: 2,
|
|
312
|
+
qquad: 4,
|
|
313
|
+
enspace: 1,
|
|
314
|
+
thinspace: 0,
|
|
315
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +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
|
+
}
|
package/tsconfig.json
ADDED
package/README.md
DELETED