2014-09-21 22:03:07 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/katex/wrapper.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
Wrapper for `katex.min.js` that provides a `<$latex>` widget. It is also available under the alias `<$katex>`
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var katex = require("$:/plugins/tiddlywiki/katex/katex.min.js"),
|
2018-12-01 13:59:08 +00:00
|
|
|
chemParse = require("$:/plugins/tiddlywiki/katex/mhchem.min.js"),
|
2014-09-21 22:03:07 +00:00
|
|
|
Widget = require("$:/core/modules/widgets/widget.js").widget;
|
2018-12-01 13:28:51 +00:00
|
|
|
// Add \ce, \pu, and \tripledash to the KaTeX macros.
|
|
|
|
katex.__defineMacro("\\ce", function(context) {
|
|
|
|
return chemParse(context.consumeArgs(1)[0], "ce")
|
|
|
|
});
|
|
|
|
katex.__defineMacro("\\pu", function(context) {
|
|
|
|
return chemParse(context.consumeArgs(1)[0], "pu");
|
|
|
|
});
|
|
|
|
// Needed for \bond for the ~ forms
|
|
|
|
// Raise by 2.56mu, not 2mu. We're raising a hyphen-minus, U+002D, not
|
|
|
|
// a mathematical minus, U+2212. So we need that extra 0.56.
|
|
|
|
katex.__defineMacro("\\tripledash", "{\\vphantom{-}\\raisebox{2.56mu}{$\\mkern2mu"
|
|
|
|
+ "\\tiny\\text{-}\\mkern1mu\\text{-}\\mkern1mu\\text{-}\\mkern2mu$}}");
|
2014-09-21 22:03:07 +00:00
|
|
|
|
|
|
|
var KaTeXWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
KaTeXWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
KaTeXWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
// Housekeeping
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
// Get the source text
|
|
|
|
var text = this.getAttribute("text",this.parseTreeNode.text || "");
|
2015-12-18 14:41:37 +00:00
|
|
|
var displayMode = this.getAttribute("displayMode",this.parseTreeNode.displayMode || "false") === "true";
|
2014-09-21 22:03:07 +00:00
|
|
|
// Render it into a span
|
2015-09-08 09:37:33 +00:00
|
|
|
var span = this.document.createElement("span"),
|
2015-12-18 14:41:37 +00:00
|
|
|
options = {throwOnError: false, displayMode: displayMode};
|
2015-07-17 07:58:58 +00:00
|
|
|
try {
|
2015-07-14 07:45:14 +00:00
|
|
|
if(!this.document.isTiddlyWikiFakeDom) {
|
2015-09-08 09:37:33 +00:00
|
|
|
katex.render(text,span,options);
|
2014-10-06 20:20:19 +00:00
|
|
|
} else {
|
2015-09-08 09:37:33 +00:00
|
|
|
span.innerHTML = katex.renderToString(text,options);
|
2014-10-06 20:20:19 +00:00
|
|
|
}
|
2015-07-17 07:58:58 +00:00
|
|
|
} catch(ex) {
|
|
|
|
span.className = "tc-error";
|
|
|
|
span.textContent = ex;
|
|
|
|
}
|
2014-09-21 22:03:07 +00:00
|
|
|
// Insert it into the DOM
|
|
|
|
parent.insertBefore(span,nextSibling);
|
|
|
|
this.domNodes.push(span);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
KaTeXWidget.prototype.execute = function() {
|
|
|
|
// Nothing to do for a katex widget
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
KaTeXWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
|
|
|
if(changedAttributes.text) {
|
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.latex = KaTeXWidget;
|
|
|
|
exports.katex = KaTeXWidget;
|
|
|
|
|
|
|
|
})();
|
|
|
|
|