Skip to content

Commit b4ccb76

Browse files
committed
Improves MathJax usage.
1 parent d34e47d commit b4ccb76

5 files changed

Lines changed: 6396 additions & 6512 deletions

File tree

math.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
11
/// <reference types="./mathjax" />
2-
//
3-
// Import the needed TeX packages
4-
//
5-
import "@mathjax/src/js/input/tex/base/BaseConfiguration.js"
6-
import "@mathjax/src/js/input/tex/ams/AmsConfiguration.js"
7-
import "@mathjax/src/js/input/tex/newcommand/NewcommandConfiguration.js"
8-
import "@mathjax/src/js/input/tex/noundefined/NoUndefinedConfiguration.js"
9-
import { liteAdaptor } from "@mathjax/src/js/adaptors/liteAdaptor"
10-
import { RegisterHTMLHandler } from "@mathjax/src/js/handlers/html"
11-
import { TeX } from "@mathjax/src/js/input/tex"
12-
import { mathjax } from "@mathjax/src/js/mathjax"
13-
import { CHTML } from "@mathjax/src/js/output/chtml"
2+
let mathjax: any
3+
async function loadMathJax() {
4+
return (mathjax ??= (await import("mathjax/tex-chtml.js")).mathjax)
5+
}
146

157
export namespace math {
16-
export async function typeset(document: string): Promise<string> {
17-
const adaptor = liteAdaptor()
18-
RegisterHTMLHandler(adaptor as any)
19-
const tex = new TeX({ packages: ["base", "ams", "newcommand", "noundefined"], inlineMath: [["$", "$"]] })
20-
const chtml = new CHTML({
21-
fontURL: "https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.0.0/es5/output/chtml/fonts/woff-v2",
22-
})
23-
const html = mathjax.document(document, { InputJax: tex, OutputJax: chtml })
24-
html.render()
25-
return adaptor.outerHTML(adaptor.root(html.document))
8+
export async function typeset(math: string): Promise<string> {
9+
const mj = await loadMathJax()
10+
const node = await mj.tex2chtmlPromise(math, { display: true })
11+
return node.outerHTML
2612
}
2713
}

mathjax.d.ts

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,18 @@
1-
declare module "@mathjax/src/js/mathjax" {
2-
interface DocumentOptions {
3-
InputJax?: any
4-
OutputJax?: any
1+
declare module "mathjax/tex-chtml.js" {
2+
interface MathJaxNode {
3+
outerHTML: string
54
[key: string]: any
65
}
76

8-
interface MathDocument {
9-
render(): void
10-
document: any
7+
interface MathJaxOptions {
8+
display?: boolean
119
[key: string]: any
1210
}
1311

14-
export const mathjax: {
15-
document(content: string, options: DocumentOptions): MathDocument
12+
interface MathJaxStatic {
13+
tex2chtmlPromise(tex: string, options?: MathJaxOptions): Promise<MathJaxNode>
1614
[key: string]: any
1715
}
18-
}
19-
20-
declare module "@mathjax/src/js/input/tex" {
21-
interface TeXOptions {
22-
packages?: string[]
23-
inlineMath?: [string, string][]
24-
[key: string]: any
25-
}
26-
27-
export class TeX {
28-
constructor(options?: TeXOptions)
29-
[key: string]: any
30-
}
31-
}
32-
33-
declare module "@mathjax/src/js/output/chtml" {
34-
interface CHtmlOptions {
35-
fontURL?: string
36-
[key: string]: any
37-
}
38-
39-
export class CHTML {
40-
constructor(options?: CHtmlOptions)
41-
[key: string]: any
42-
}
43-
}
44-
45-
declare module "@mathjax/src/js/adaptors/liteAdaptor" {
46-
interface LiteAdaptor {
47-
outerHTML(element: any): string
48-
root(document: any): any
49-
[key: string]: any
50-
}
51-
52-
export function liteAdaptor(): LiteAdaptor
53-
}
54-
55-
declare module "@mathjax/src/js/handlers/html" {
56-
export function RegisterHTMLHandler(adaptor: any): void
57-
}
5816

59-
declare module "@mathjax/src/js/input/tex/AllPackages" {
60-
export const AllPackages: string[]
17+
export const mathjax: MathJaxStatic
6118
}

mathjax.mjs

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)