-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
index.ts
185 lines (158 loc) · 4.62 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/**
* @packageDocumentation
* @module mathjax-extension
*/
import { PromiseDelegate } from '@lumino/coreutils';
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { ILatexTypesetter } from '@jupyterlab/rendermime';
import type { MathDocument } from 'mathjax-full/js/core/MathDocument';
namespace CommandIDs {
/**
* Copy raw LaTeX to clipboard.
*/
export const copy = 'mathjax:clipboard';
/**
* Scale MathJax elements.
*/
export const scale = 'mathjax:scale';
}
namespace CommandArgs {
export type scale = {
scale: number;
};
}
/**
* The MathJax Typesetter.
*/
export class MathJaxTypesetter implements ILatexTypesetter {
protected async _ensureInitialized() {
if (!this._initialized) {
this._mathDocument = await Private.ensureMathDocument();
this._initialized = true;
}
}
/**
* Get an instance of the MathDocument object.
*/
async mathDocument(): Promise<MathDocument<any, any, any>> {
await this._ensureInitialized();
return this._mathDocument;
}
/**
* Typeset the math in a node.
*/
async typeset(node: HTMLElement): Promise<void> {
try {
await this._ensureInitialized();
} catch (e) {
console.error(e);
return;
}
this._mathDocument.options.elements = [node];
this._mathDocument.clear().render();
delete this._mathDocument.options.elements;
}
protected _initialized: boolean = false;
protected _mathDocument: MathDocument<any, any, any>;
}
/**
* The MathJax extension.
*/
const mathJaxPlugin: JupyterFrontEndPlugin<ILatexTypesetter> = {
id: '@jupyterlab/mathjax-extension:plugin',
description: 'Provides the LaTeX mathematical expression interpreter.',
provides: ILatexTypesetter,
activate: (app: JupyterFrontEnd) => {
const typesetter = new MathJaxTypesetter();
app.commands.addCommand(CommandIDs.copy, {
execute: async () => {
const md = await typesetter.mathDocument();
const oJax: any = md.outputJax;
await navigator.clipboard.writeText(oJax.math.math);
},
label: 'MathJax Copy Latex'
});
app.commands.addCommand(CommandIDs.scale, {
execute: async (args: CommandArgs.scale) => {
const md = await typesetter.mathDocument();
const scale = args['scale'] || 1.0;
md.outputJax.options.scale = scale;
md.rerender();
},
label: args =>
'Mathjax Scale ' + (args['scale'] ? `x${args['scale']}` : 'Reset')
});
return typesetter;
},
autoStart: true
};
export default mathJaxPlugin;
/**
* A namespace for module-private functionality.
*/
namespace Private {
let _loading: PromiseDelegate<MathDocument<any, any, any>> | null = null;
export async function ensureMathDocument(): Promise<
MathDocument<any, any, any>
> {
if (!_loading) {
_loading = new PromiseDelegate();
void import('mathjax-full/js/input/tex/require/RequireConfiguration');
const [
{ mathjax },
{ CHTML },
{ TeX },
{ TeXFont },
{ AllPackages },
{ SafeHandler },
{ HTMLHandler },
{ browserAdaptor },
{ AssistiveMmlHandler }
] = await Promise.all([
import('mathjax-full/js/mathjax'),
import('mathjax-full/js/output/chtml'),
import('mathjax-full/js/input/tex'),
import('mathjax-full/js/output/chtml/fonts/tex'),
import('mathjax-full/js/input/tex/AllPackages'),
import('mathjax-full/js/ui/safe/SafeHandler'),
import('mathjax-full/js/handlers/html/HTMLHandler'),
import('mathjax-full/js/adaptors/browserAdaptor'),
import('mathjax-full/js/a11y/assistive-mml')
]);
mathjax.handlers.register(
AssistiveMmlHandler(SafeHandler(new HTMLHandler(browserAdaptor())))
);
class EmptyFont extends TeXFont {
protected static defaultFonts = {} as any;
}
const chtml = new CHTML({
// Override dynamically generated fonts in favor of our font css
font: new EmptyFont()
});
const tex = new TeX({
packages: AllPackages.concat('require'),
inlineMath: [
['$', '$'],
['\\(', '\\)']
],
displayMath: [
['$$', '$$'],
['\\[', '\\]']
],
processEscapes: true,
processEnvironments: true
});
const mathDocument = mathjax.document(window.document, {
InputJax: tex,
OutputJax: chtml
});
_loading.resolve(mathDocument);
}
return _loading.promise;
}
}