forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
117 lines (104 loc) · 3.22 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
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import { JupyterLabPlugin } from '@jupyterlab/application';
import { PageConfig } from '@jupyterlab/coreutils';
import { ILatexTypesetter } from '@jupyterlab/rendermime';
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
import { PromiseDelegate } from '@phosphor/coreutils';
// Stub for window MathJax.
declare var MathJax: any;
import '../style/index.css';
/**
* The MathJax latexTypesetter plugin.
*/
const plugin: JupyterLabPlugin<ILatexTypesetter> = {
id: '@jupyterlab/mathjax2-extension:plugin',
autoStart: true,
provides: ILatexTypesetter,
activate: () => new MathJaxTypesetter()
};
/**
* Export the plugin as default.
*/
export default plugin;
/**
* The MathJax Typesetter.
*/
export class MathJaxTypesetter implements IRenderMime.ILatexTypesetter {
/**
* 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();
}
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 url = PageConfig.getOption('mathjaxUrl');
let config = PageConfig.getOption('mathjaxConfig');
let head = document.getElementsByTagName('head')[0];
let script = document.createElement('script');
script.type = 'text/javascript';
script.src = `${url}?config=${config}&delayStartupUntil=configured" 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;
}