From 63c174d7ed56284e80ad6cd6ae966b81f9181cc9 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Mon, 6 Oct 2014 21:20:19 +0100 Subject: [PATCH] Update KaTeX wrapper to support operation under Node.js Now we can generate static HTML containing mathematical typesetting --- core/modules/utils/fakedom.js | 4 +++- editions/katexdemo/tiddlers/HelloThere.tid | 4 +--- editions/katexdemo/tiddlywiki.info | 7 ++++++- plugins/tiddlywiki/katex/files/tiddlywiki.files | 4 +++- plugins/tiddlywiki/katex/wrapper.js | 6 +++++- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/core/modules/utils/fakedom.js b/core/modules/utils/fakedom.js index d7cb633b7..1c4bf2755 100755 --- a/core/modules/utils/fakedom.js +++ b/core/modules/utils/fakedom.js @@ -143,7 +143,9 @@ Object.defineProperty(TW_Element.prototype, "outerHTML", { for(var s in this.style) { style.push(s + ":" + this.style[s] + ";"); } - output.push(" style='",style.join(""),"'") + if(style.length > 0) { + output.push(" style='",style.join(""),"'") + } } output.push(">"); if($tw.config.htmlVoidElements.indexOf(this.tag) === -1) { diff --git a/editions/katexdemo/tiddlers/HelloThere.tid b/editions/katexdemo/tiddlers/HelloThere.tid index 719121c1e..c3ecef5bb 100644 --- a/editions/katexdemo/tiddlers/HelloThere.tid +++ b/editions/katexdemo/tiddlers/HelloThere.tid @@ -2,9 +2,7 @@ title: HelloThere This is a TiddlyWiki plugin for mathematical typesetting based on KaTeX from Khan Academy. -It is completely self-contained, and doesn't need an Internet connection in order to work. - -//This first version of the plugin cannot be used to generate static content under Node.js, but that capability will come in a future update. (Note that you can still use it when running the client-server configuration under Node.js).// +It is completely self-contained, and doesn't need an Internet connection in order to work. It works both in the browser and under Node.js. ! Installation diff --git a/editions/katexdemo/tiddlywiki.info b/editions/katexdemo/tiddlywiki.info index 8f24a036e..b45c7b950 100644 --- a/editions/katexdemo/tiddlywiki.info +++ b/editions/katexdemo/tiddlywiki.info @@ -10,6 +10,11 @@ ], "build": { "index": [ - "--rendertiddler","$:/core/save/all","katexdemo.html","text/plain"] + "--rendertiddler","$:/core/save/all","katexdemo.html","text/plain"], + "static": [ + "--rendertiddler","$:/core/templates/static.template.html","static.html","text/plain", + "--rendertiddler","$:/core/templates/alltiddlers.template.html","alltiddlers.html","text/plain", + "--rendertiddlers","[!is[system]]","$:/core/templates/static.tiddler.html","static","text/plain", + "--rendertiddler","$:/core/templates/static.template.css","static/static.css","text/plain"] } } diff --git a/plugins/tiddlywiki/katex/files/tiddlywiki.files b/plugins/tiddlywiki/katex/files/tiddlywiki.files index 1c290da42..522f9e7fe 100644 --- a/plugins/tiddlywiki/katex/files/tiddlywiki.files +++ b/plugins/tiddlywiki/katex/files/tiddlywiki.files @@ -78,7 +78,9 @@ "type": "application/javascript", "title": "$:/plugins/tiddlywiki/katex/katex.min.js", "module-type": "library" - } + }, + "prefix": "(function(document) {\n", + "suffix": "\n})($tw.node ? $tw.fakeDocument : window.document)\n" } ] } diff --git a/plugins/tiddlywiki/katex/wrapper.js b/plugins/tiddlywiki/katex/wrapper.js index 517ff5950..1e91d5223 100644 --- a/plugins/tiddlywiki/katex/wrapper.js +++ b/plugins/tiddlywiki/katex/wrapper.js @@ -37,7 +37,11 @@ KaTeXWidget.prototype.render = function(parent,nextSibling) { // Render it into a span var span = this.document.createElement("span"); try { - katex.render(text,span); + if($tw.browser) { + katex.render(text,span); + } else { + span.innerHTML = katex.renderToString(text); + } } catch(ex) { span.className = "tc-error"; span.textContent = ex;