-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderers.ts
More file actions
70 lines (62 loc) · 2.88 KB
/
Copy pathrenderers.ts
File metadata and controls
70 lines (62 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* yalt + KaTeX / MathJax / Temml
*
* yalt is renderer-agnostic. `toRenderInput` returns `{ tex, displayMode }`
* which maps directly to every major math renderer's API.
*
* Run: npx tsx examples/renderers.ts
* (Only the KaTeX section runs - MathJax and Temml sections are
* reference patterns for browser use.)
*/
import katex from 'katex';
// --- yalt: import one-shot parse and the renderer helper ---
import { parse, toRenderInput } from 'yalt';
const input =
'Inline: $E = mc^2$. Display: $$\\int_0^\\infty e^{-x}\\,dx = 1$$' +
' Environment: \\begin{align} a &= b \\\\ c &= d \\end{align}';
// --- yalt: parse the input into text + math events ---
const events = parse(input);
const mathEvents = events.filter((e) => e.type === 'math');
// ── KaTeX ───────────────────────────────────────────────────────────
// npm install katex
// KaTeX uses `displayMode` (boolean).
console.log('── KaTeX ──\n');
for (const event of mathEvents) {
// --- yalt: toRenderInput returns { tex, displayMode } for any renderer ---
const { tex, displayMode } = toRenderInput(event);
const html = katex.renderToString(tex, {
displayMode,
throwOnError: false,
});
console.log(` ${displayMode ? 'block' : 'inline'}: ${tex}`);
console.log(` html: ${html.slice(0, 80)}…\n`);
}
// ── MathJax v3 (browser) ───────────────────────────────────────────
// <script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js"></script>
// MathJax uses `display` (boolean), not `displayMode`.
//
// const { tex, displayMode } = toRenderInput(event);
// const node = MathJax.tex2svg(tex, { display: displayMode });
// container.append(node);
//
// Or with CHTML output:
// const node = MathJax.tex2chtml(tex, { display: displayMode });
// container.append(node);
console.log('── MathJax v3 (reference) ──\n');
for (const event of mathEvents) {
const { tex, displayMode } = toRenderInput(event);
console.log(` MathJax.tex2svg('${tex}', { display: ${displayMode} })`);
}
// ── Temml ───────────────────────────────────────────────────────────
// npm install temml
// Temml renders to MathML (native browser rendering, no fonts needed).
// Temml uses `displayMode` (boolean), same as KaTeX.
//
// import temml from 'temml';
// const { tex, displayMode } = toRenderInput(event);
// const html = temml.renderToString(tex, { displayMode });
console.log('\n── Temml (reference) ──\n');
for (const event of mathEvents) {
const { tex, displayMode } = toRenderInput(event);
console.log(` temml.renderToString('${tex}', { displayMode: ${displayMode} })`);
}