@jupyterlab/mathjax-extension
Version:
A JupyterLab extension providing MathJax Typesetting
139 lines • 4.75 kB
JavaScript
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/**
* @packageDocumentation
* @module mathjax-extension
*/
import { PromiseDelegate } from '@lumino/coreutils';
import { ILatexTypesetter } from '@jupyterlab/rendermime';
var CommandIDs;
(function (CommandIDs) {
/**
* Copy raw LaTeX to clipboard.
*/
CommandIDs.copy = 'mathjax:clipboard';
/**
* Scale MathJax elements.
*/
CommandIDs.scale = 'mathjax:scale';
})(CommandIDs || (CommandIDs = {}));
/**
* The MathJax Typesetter.
*/
export class MathJaxTypesetter {
constructor() {
this._initialized = false;
}
async _ensureInitialized() {
if (!this._initialized) {
this._mathDocument = await Private.ensureMathDocument();
this._initialized = true;
}
}
/**
* Get an instance of the MathDocument object.
*/
async mathDocument() {
await this._ensureInitialized();
return this._mathDocument;
}
/**
* Typeset the math in a node.
*/
async typeset(node) {
try {
await this._ensureInitialized();
}
catch (e) {
console.error(e);
return;
}
this._mathDocument.options.elements = [node];
this._mathDocument.clear().render();
delete this._mathDocument.options.elements;
}
}
/**
* The MathJax extension.
*/
const mathJaxPlugin = {
id: '@jupyterlab/mathjax-extension:plugin',
description: 'Provides the LaTeX mathematical expression interpreter.',
provides: ILatexTypesetter,
activate: (app) => {
const typesetter = new MathJaxTypesetter();
app.commands.addCommand(CommandIDs.copy, {
execute: async () => {
const md = await typesetter.mathDocument();
const oJax = md.outputJax;
await navigator.clipboard.writeText(oJax.math.math);
},
label: 'MathJax Copy Latex'
});
app.commands.addCommand(CommandIDs.scale, {
execute: async (args) => {
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.
*/
var Private;
(function (Private) {
let _loading = null;
async function ensureMathDocument() {
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 {
}
EmptyFont.defaultFonts = {};
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;
}
Private.ensureMathDocument = ensureMathDocument;
})(Private || (Private = {}));
//# sourceMappingURL=index.js.map