From 710fae5fe3af6a06622ee9e44c4acb4ed4df8e08 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Sun, 13 Nov 2022 21:42:27 -0600 Subject: [PATCH] Upgrade to MathJax 3 --- resources/dmmd-preview.js | 12 +++--------- resources/mathjax_config.js | 29 +++++++++++++++++++++-------- resources/pagedown_math.js | 4 ++-- templates/chat/chat.html | 4 ++-- templates/comments/media-js.html | 7 ++----- templates/mathjax-load.html | 12 +----------- 6 files changed, 31 insertions(+), 37 deletions(-) diff --git a/resources/dmmd-preview.js b/resources/dmmd-preview.js index fa6dc87..4c16839 100644 --- a/resources/dmmd-preview.js +++ b/resources/dmmd-preview.js @@ -36,23 +36,17 @@ $(function () { success: function () { $.ajax({ type: 'GET', - url: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_HTML', + url: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js', dataType: 'script', cache: true, success: function () { - MathJax.Hub.Queue(function () { - $content.find('.tex-image').hide(); - $content.find('.tex-text').show(); - }); + MathJax.typeset(); } }); } }); } else { - MathJax.Hub.Queue(['Typeset', MathJax.Hub, $content[0]], function () { - $content.find('.tex-image').hide(); - $content.find('.tex-text').show(); - }); + MathJax.typeset($content[0]); } } }); diff --git a/resources/mathjax_config.js b/resources/mathjax_config.js index 1c3f70a..0ea5c70 100644 --- a/resources/mathjax_config.js +++ b/resources/mathjax_config.js @@ -1,10 +1,23 @@ window.MathJax = { - messageStyle: 'none', - tex2jax: { - inlineMath: [ - ['$', '$'], - ['\\(', '\\)'] - ] - }, - showMathMenu: false + options: { + ignoreHtmlClass: 'tex2jax_ignore', + processHtmlClass: 'tex2jax_process', + renderActions: { + find: [10, function (doc) { + for (const node of document.querySelectorAll('script[type^="math/tex"]')) { + const display = !!node.type.match(/; *mode=display/); + const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display); + const text = document.createTextNode(''); + const sibling = node.previousElementSibling; + node.parentNode.replaceChild(text, node); + math.start = {node: text, delim: '', n: 0}; + math.end = {node: text, delim: '', n: 0}; + doc.math.push(math); + if (sibling && sibling.matches('.MathJax_Preview')) { + sibling.parentNode.removeChild(sibling); + } + } + }, ''] + } + } }; \ No newline at end of file diff --git a/resources/pagedown_math.js b/resources/pagedown_math.js index dfdd963..4ebf484 100644 --- a/resources/pagedown_math.js +++ b/resources/pagedown_math.js @@ -3,9 +3,9 @@ function mathjax_pagedown($) { $.each(window.editors, function (id, editor) { var preview = $('div.wmd-preview#' + id + '_wmd_preview')[0]; editor.hooks.chain('onPreviewRefresh', function () { - MathJax.Hub.Queue(["Typeset", MathJax.Hub, preview]); + MathJax.typeset(preview); }); - MathJax.Hub.Queue(["Typeset", MathJax.Hub, preview]); + MathJax.typeset(preview); }); } } diff --git a/templates/chat/chat.html b/templates/chat/chat.html index 46dcde9..87a6077 100644 --- a/templates/chat/chat.html +++ b/templates/chat/chat.html @@ -156,7 +156,7 @@ let META_HEADER = [ $('#chat-log').append($data); $('#chat-box').scrollTop($('#chat-box')[0].scrollHeight); register_time($('.time-with-rel')); - MathJax.Hub.Queue(["Typeset",MathJax.Hub]); + MathJax.typeset(); merge_authors(); } @@ -210,7 +210,7 @@ let META_HEADER = [ else { add_new_message(message, room, true); } - MathJax.Hub.Queue(["Typeset",MathJax.Hub]); + MathJax.typeset(); register_time($('.time-with-rel')); remove_unread_current_user(); merge_authors(); diff --git a/templates/comments/media-js.html b/templates/comments/media-js.html index 8789e95..1ca2d4c 100644 --- a/templates/comments/media-js.html +++ b/templates/comments/media-js.html @@ -55,10 +55,7 @@ function update_math($comment) { if ('MathJax' in window) { var $body = $comment.find('.comment-body'); - MathJax.Hub.Queue(['Typeset', MathJax.Hub, $body[0]], function () { - $body.find('.tex-image').hide(); - $body.find('.tex-text').show(); - }); + MathJax.typeset($body[0]); } } @@ -168,7 +165,7 @@ window.DjangoPagedown.createEditor($wmd.get(0)); if ('MathJax' in window) { var preview = $('.featherlight div.wmd-preview')[0]; - MathJax.Hub.Queue(['Typeset', MathJax.Hub, preview]); + MathJax.typeset(preview); } } } diff --git a/templates/mathjax-load.html b/templates/mathjax-load.html index 19415ed..27878dc 100644 --- a/templates/mathjax-load.html +++ b/templates/mathjax-load.html @@ -1,12 +1,2 @@ - - +