-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkatex.ts
65 lines (58 loc) · 1.58 KB
/
katex.ts
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
import { katex } from "./dependencies.ts";
import { html5_dependency_css } from "./html5.ts";
import {
Expression,
Invocation,
is_expression,
new_macro,
} from "./tsgen.ts";
export function $(fst: Expression, snd?: Expression, third?: Expression) : Expression {
const macro = new_macro(
(args, _ctx) => {
let pre = null;
let math = args[0];
let post = null;
if (args.length === 3) {
pre = args[0];
math = args[1];
post = args[2];
} else if (args.length === 2) {
post = args[1];
}
return [html5_dependency_css("/named_assets/katex.min.css"), [
pre ? ["\\htmlClass{normal_text}{\\text{", pre, "}}"] : "",
math,
post ? ["\\htmlClass{normal_text}{\\text{", post, "}}"] : "",
]];
},
(expanded, ctx) => {
try {
return katex.default.renderToString(expanded, {
displayMode: false,
throwOnError: true,
output: "html",
strict: false,
trust: true,
});
} catch (err) {
ctx.error("Failed to render katex:");
ctx.error(err);
ctx.halt();
return null;
}
}
);
if (third) {
return new Invocation(macro, [fst, snd!, third]);
} else if (snd) {
return new Invocation(macro, [fst, snd]);
} else {
return new Invocation(macro, [fst]);
}
}
export function $dot(content: Expression): Expression {
return $(content, ".");
}
export function $comma(content: Expression): Expression {
return $(content, ",");
}