From b8854a287ac92ca941df6cb1ab6320b6ffe3232a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Sun, 3 Apr 2016 14:32:17 +0200 Subject: [PATCH] Switch math rendering to KaTeX --- index.html | 2 +- js/filters.js | 18 +++++++++++++----- js/glowingbear.js | 30 +++++++----------------------- js/utils.js | 25 ++++++++++++++++++++++++- 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/index.html b/index.html index 3bfdb50..4db5a59 100644 --- a/index.html +++ b/index.html @@ -295,7 +295,7 @@ $ openssl req -nodes -newkey rsa:4096 -keyout relay.pem -x509 -days 365 -out rel <>
+ --> diff --git a/js/filters.js b/js/filters.js index e51f0a2..37ff9b9 100644 --- a/js/filters.js +++ b/js/filters.js @@ -175,15 +175,23 @@ weechat.filter('emojify', function() { }; }); -weechat.filter('mathjax', function() { +weechat.filter('latexmath', function() { return function(text, selector, enabled) { - if (!enabled || typeof(MathJax) === "undefined") { + if (!enabled || typeof(katex) === "undefined") { return text; } if (text.indexOf("$$") != -1 || text.indexOf("\\[") != -1 || text.indexOf("\\(") != -1) { - // contains math - var math = document.querySelector(selector); - MathJax.Hub.Queue(["Typeset",MathJax.Hub,math]); + // contains math -> delayed rendering + setTimeout(function() { + var math = document.querySelector(selector); + renderMathInElement(math, { + delimiters: [ + {left: "$$", right: "$$", display: false}, + {left: "\\[", right: "\\]", display: true}, + {left: "\\(", right: "\\)", display: false} + ] + }); + }); } return text; diff --git a/js/glowingbear.js b/js/glowingbear.js index 271916a..b2ee72f 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -382,26 +382,17 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', }); // To prevent unnecessary loading times for users who don't - // want MathJax, load it only if the setting is enabled. + // want LaTeX math, load it only if the setting is enabled. // This also fires when the page is loaded if enabled. + // Note that this says MathJax but we switched to KaTeX settings.addCallback('enableMathjax', function(enabled) { if (enabled && !$rootScope.mathjax_init) { // Load MathJax only once $rootScope.mathjax_init = true; - (function () { - var head = document.getElementsByTagName("head")[0], script; - script = document.createElement("script"); - script.type = "text/x-mathjax-config"; - script[(window.opera ? "innerHTML" : "text")] = - "MathJax.Hub.Config({\n" + - " tex2jax: { inlineMath: [['$$','$$'], ['\\\\(','\\\\)']], displayMath: [['\\\\[','\\\\]']] },\n" + - "});"; - head.appendChild(script); - script = document.createElement("script"); - script.type = "text/javascript"; - script.src = "//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"; - head.appendChild(script); - })(); + + utils.inject_css("https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.5.1/katex.min.css"); + utils.inject_script("https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.5.1/katex.min.js"); + utils.inject_script("https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.5.1/contrib/auto-render.min.js"); } }); @@ -415,14 +406,7 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', } // Load new theme - (function() { - var elem = document.createElement("link"); - elem.rel = "stylesheet"; - elem.href = "css/themes/" + theme + ".css"; - elem.media = "screen"; - elem.id = "themeCSS"; - document.getElementsByTagName("head")[0].appendChild(elem); - })(); + utils.inject_css("css/themes/" + theme + ".css", "themeCSS"); }); settings.addCallback('customCSS', function(css) { diff --git a/js/utils.js b/js/utils.js index 5a10c7a..350f5c0 100644 --- a/js/utils.js +++ b/js/utils.js @@ -21,9 +21,32 @@ weechat.factory('utils', function() { return (document.body.clientWidth < mobile_cutoff); }; + + // Inject a javascript (used by KaTeX) + var inject_script = function(script_url) { + var script = document.createElement("script"); + script.type = "text/javascript"; + script.src = script_url; + var head = document.getElementsByTagName("head")[0]; + head.appendChild(script); + }; + // Inject a stylesheet (used by KaTeX and theme switching) + var inject_css = function(css_url, id) { + var elem = document.createElement("link"); + elem.rel = "stylesheet"; + elem.href = css_url; + if (id) + elem.id = id; + var head = document.getElementsByTagName("head")[0]; + head.appendChild(elem); + }; + + return { changeClassStyle: changeClassStyle, getClassStyle: getClassStyle, - isMobileUi: isMobileUi + isMobileUi: isMobileUi, + inject_script: inject_script, + inject_css: inject_css, }; });