/
index.ts
129 lines (119 loc) · 3.26 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
import { PromiseDelegate } from '@phosphor/coreutils';
// Stub for window MathJax.
declare var MathJax: any;
/**
* The MathJax Typesetter.
*/
export class MathJaxTypesetter implements IRenderMime.ILatexTypesetter {
/**
* Create a new MathJax typesetter.
*/
constructor(options: MathJaxTypesetter.IOptions) {
this._url = options.url;
this._config = options.config;
}
/**
* Typeset the math in a node.
*
* #### Notes
* MathJax schedules the typesetting asynchronously,
* but there are not currently any callbacks or Promises
* firing when it is done.
*/
typeset(node: HTMLElement): void {
if (!this._initialized) {
this._init();
}
void this._initPromise.promise.then(() => {
MathJax.Hub.Queue(['Typeset', MathJax.Hub, node]);
try {
MathJax.Hub.Queue(
['Require', MathJax.Ajax, '[MathJax]/extensions/TeX/AMSmath.js'],
() => {
MathJax.InputJax.TeX.resetEquationNumbers();
}
);
} catch (e) {
console.error('Error queueing resetEquationNumbers:', e);
}
});
}
/**
* Initialize MathJax.
*/
private _init(): void {
let head = document.getElementsByTagName('head')[0];
let script = document.createElement('script');
script.type = 'text/javascript';
script.src = `${this._url}?config=${this._config}&delayStartupUntil=configured`;
script.charset = 'utf-8';
head.appendChild(script);
script.addEventListener('load', () => {
this._onLoad();
});
this._initialized = true;
}
/**
* Handle MathJax loading.
*/
private _onLoad(): void {
MathJax.Hub.Config({
tex2jax: {
inlineMath: [
['$', '$'],
['\\(', '\\)']
],
displayMath: [
['$$', '$$'],
['\\[', '\\]']
],
processEscapes: true,
processEnvironments: true
},
// Center justify equations in code and markdown cells. Elsewhere
// we use CSS to left justify single line equations in code cells.
displayAlign: 'center',
CommonHTML: {
linebreaks: { automatic: true }
},
'HTML-CSS': {
availableFonts: [],
imageFont: null,
preferredFont: null,
webFont: 'STIX-Web',
styles: { '.MathJax_Display': { margin: 0 } },
linebreaks: { automatic: true }
},
skipStartupTypeset: true
});
MathJax.Hub.Configured();
this._initPromise.resolve(void 0);
}
private _initPromise = new PromiseDelegate<void>();
private _initialized = false;
private _url: string;
private _config: string;
}
/**
* Namespace for MathJaxTypesetter.
*/
export namespace MathJaxTypesetter {
/**
* MathJaxTypesetter constructor options.
*/
export interface IOptions {
/**
* The url to load MathJax from.
*/
url: string;
/**
* A configuration string to compose into the MathJax URL.
*/
config: string;
}
}