From 08d2437d49f9f09d13e02c7f8476af6d50e861b0 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Mon, 5 Feb 2024 17:02:49 -0600 Subject: [PATCH] Move from mathjax to katex --- judge/forms.py | 3 +- judge/management/commands/render_pdf.py | 2 +- judge/markdown.py | 17 +------- judge/views/comment.py | 2 +- judge/views/problem.py | 2 - judge/widgets/pagedown.py | 22 ++++++----- resources/base.scss | 10 ----- resources/dmmd-preview.js | 26 +------------ resources/katex_config.js | 17 ++++++++ resources/mathjax3_config.js | 26 ------------- resources/pagedown_math.js | 15 ++++--- templates/base.html | 2 + templates/blog/blog.html | 3 -- templates/chat/chat.html | 3 -- templates/chat/chat_js.html | 2 +- templates/comments/media-js.html | 39 +++---------------- templates/comments/preview.html | 5 +-- templates/common-content.html | 8 +--- templates/contest/preview.html | 3 -- templates/feed/feed_js.html | 2 +- templates/flatpages/markdown_math.html | 4 -- templates/katex-load.html | 4 ++ .../markdown_editor/markdown_editor.html | 4 +- templates/mathjax-load.html | 5 --- templates/organization/preview.html | 3 -- templates/problem/editorial.html | 3 -- templates/problem/preview.html | 5 +-- templates/problem/raw.html | 13 +------ templates/solution-preview.html | 3 -- .../test_formatter/edit_test_formatter.html | 3 -- templates/three-column-content.html | 5 +-- templates/ticket/preview.html | 5 +-- templates/ticket/ticket.html | 3 -- templates/user/preview.html | 5 +-- templates/user/user-about.html | 4 -- 35 files changed, 64 insertions(+), 214 deletions(-) create mode 100644 resources/katex_config.js delete mode 100644 resources/mathjax3_config.js create mode 100644 templates/katex-load.html delete mode 100644 templates/mathjax-load.html diff --git a/judge/forms.py b/judge/forms.py index c52eabe..4c5b49e 100644 --- a/judge/forms.py +++ b/judge/forms.py @@ -43,7 +43,6 @@ from judge.models import ( from judge.widgets import ( HeavyPreviewPageDownWidget, - MathJaxPagedownWidget, PagedownWidget, Select2MultipleWidget, Select2Widget, @@ -412,7 +411,7 @@ class NewMessageForm(ModelForm): fields = ["title", "content"] widgets = {} if PagedownWidget is not None: - widgets["content"] = MathJaxPagedownWidget() + widgets["content"] = PagedownWidget() class CustomAuthenticationForm(AuthenticationForm): diff --git a/judge/management/commands/render_pdf.py b/judge/management/commands/render_pdf.py index 7dc9e33..7900420 100644 --- a/judge/management/commands/render_pdf.py +++ b/judge/management/commands/render_pdf.py @@ -96,7 +96,7 @@ class Command(BaseCommand): .replace("'//", "'https://") ) maker.title = problem_name - for file in ("style.css", "mathjax3_config.js"): + for file in "style.css": maker.load(file, os.path.join(settings.DMOJ_RESOURCES, file)) maker.make(debug=True) if not maker.success: diff --git a/judge/markdown.py b/judge/markdown.py index 4f37cfb..ba594c1 100644 --- a/judge/markdown.py +++ b/judge/markdown.py @@ -6,7 +6,7 @@ from pymdownx import superfences EXTENSIONS = [ - "pymdownx.arithmatex", + # "pymdownx.arithmatex", "pymdownx.magiclink", "pymdownx.betterem", "pymdownx.details", @@ -83,24 +83,9 @@ def markdown(value, lazy_load=False): value, extensions=extensions, extension_configs=EXTENSION_CONFIGS ) - # Don't clean mathjax - hash_script_tag = {} soup = BeautifulSoup(html, "html.parser") - for script_tag in soup.find_all("script"): - allow_math_types = ["math/tex", "math/tex; mode=display"] - if script_tag.attrs.get("type", False) in allow_math_types: - hash_script_tag[str(hash(str(script_tag)))] = str(script_tag) - - for hashed_tag in hash_script_tag: - tag = hash_script_tag[hashed_tag] - html = html.replace(tag, hashed_tag) - html = bleach.clean(html, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRS) - for hashed_tag in hash_script_tag: - tag = hash_script_tag[hashed_tag] - html = html.replace(hashed_tag, tag) - if not html: html = escape(value) if lazy_load: diff --git a/judge/views/comment.py b/judge/views/comment.py index 9e5a986..15009f2 100644 --- a/judge/views/comment.py +++ b/judge/views/comment.py @@ -27,7 +27,7 @@ from django_ratelimit.decorators import ratelimit from judge.models import Comment, CommentVote, Notification, BlogPost from judge.utils.views import TitleMixin -from judge.widgets import MathJaxPagedownWidget, HeavyPreviewPageDownWidget +from judge.widgets import HeavyPreviewPageDownWidget from judge.comments import add_mention_notifications import json diff --git a/judge/views/problem.py b/judge/views/problem.py index d86909a..7230d12 100644 --- a/judge/views/problem.py +++ b/judge/views/problem.py @@ -407,8 +407,6 @@ class ProblemPdfView(ProblemMixin, SingleObjectMixin, View): ) maker.title = problem_name assets = ["style.css"] - if maker.math_engine == "jax": - assets.append("mathjax3_config.js") for file in assets: maker.load(file, os.path.join(settings.DMOJ_RESOURCES, file)) maker.make() diff --git a/judge/widgets/pagedown.py b/judge/widgets/pagedown.py index e0f812c..b0403d5 100644 --- a/judge/widgets/pagedown.py +++ b/judge/widgets/pagedown.py @@ -10,8 +10,8 @@ from judge.widgets.mixins import CompressorWidgetMixin __all__ = [ "PagedownWidget", "AdminPagedownWidget", - "MathJaxPagedownWidget", - "MathJaxAdminPagedownWidget", + "KatexPagedownWidget", + "KatexAdminPagedownWidget", "HeavyPreviewPageDownWidget", "HeavyPreviewAdminPageDownWidget", ] @@ -21,8 +21,8 @@ try: except ImportError: PagedownWidget = None AdminPagedownWidget = None - MathJaxPagedownWidget = None - MathJaxAdminPagedownWidget = None + KatexPagedownWidget = None + KatexAdminPagedownWidget = None HeavyPreviewPageDownWidget = None HeavyPreviewAdminPageDownWidget = None else: @@ -61,15 +61,19 @@ else: } js = ["admin/js/pagedown.js"] - class MathJaxPagedownWidget(PagedownWidget): + class KatexPagedownWidget(PagedownWidget): class Media: + css = { + "all": ["https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css"] + } js = [ - "mathjax3_config.js", - "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js", + "katex_config.js", + "https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js", + "https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js", "pagedown_math.js", ] - class MathJaxAdminPagedownWidget(AdminPagedownWidget, MathJaxPagedownWidget): + class KatexAdminPagedownWidget(AdminPagedownWidget, KatexPagedownWidget): pass class HeavyPreviewPageDownWidget(PagedownWidget): @@ -112,7 +116,7 @@ else: js = ["dmmd-preview.js"] class HeavyPreviewAdminPageDownWidget( - AdminPagedownWidget, HeavyPreviewPageDownWidget + KatexPagedownWidget, AdminPagedownWidget, HeavyPreviewPageDownWidget ): class Media: css = { diff --git a/resources/base.scss b/resources/base.scss index aee3a97..49235f1 100644 --- a/resources/base.scss +++ b/resources/base.scss @@ -521,16 +521,6 @@ noscript #noscript { margin-top: 1.2em; } -math { - font-size: 1.155em; -} - -.MathJax { - &:focus { - outline: none; - } -} - @media(max-width: 1498px) { #page-container { border-left: none; diff --git a/resources/dmmd-preview.js b/resources/dmmd-preview.js index 2450ea8..4efdc88 100644 --- a/resources/dmmd-preview.js +++ b/resources/dmmd-preview.js @@ -30,31 +30,7 @@ $(function () { $(this).attr("src", $(this).attr("data-src")); }) $preview.addClass('dmmd-preview-has-content').removeClass('dmmd-preview-stale'); - - var $jax = $content.find('.require-mathjax-support'); - if ($jax.length) { - if (!('MathJax' in window)) { - $.ajax({ - type: 'GET', - url: $jax.attr('data-config'), - dataType: 'script', - cache: true, - success: function () { - $.ajax({ - type: 'GET', - url: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js', - dataType: 'script', - cache: true, - success: function () { - MathJax.typeset(); - } - }); - } - }); - } else { - MathJax.typeset($content[0]); - } - } + renderKatex($content[0]); }); } else { $content.empty(); diff --git a/resources/katex_config.js b/resources/katex_config.js new file mode 100644 index 0000000..0a18883 --- /dev/null +++ b/resources/katex_config.js @@ -0,0 +1,17 @@ +window.KatexOptions = { + delimiters: [ + {left: '$$', right: '$$', display: true}, + {left: '$', right: '$', display: false}, + {left: '\\[', right: '\\]', display: true}, + {left: "\\(", right: "\\)", display: false}, + {left: "\\begin{equation}", right: "\\end{equation}", display: true}, + {left: "\\begin{align}", right: "\\end{align}", display: true}, + {left: "\\begin{alignat}", right: "\\end{alignat}", display: true}, + {left: "\\begin{gather}", right: "\\end{gather}", display: true}, + {left: "\\begin{CD}", right: "\\end{CD}", display: true}, + ], + throwOnError : false +}; +window.renderKatex = (elem=document.body) => { + renderMathInElement(elem, window.KatexOptions); +} \ No newline at end of file diff --git a/resources/mathjax3_config.js b/resources/mathjax3_config.js deleted file mode 100644 index 6748a69..0000000 --- a/resources/mathjax3_config.js +++ /dev/null @@ -1,26 +0,0 @@ -window.MathJax = { - chtml: { - adaptiveCSS: 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 febfd8d..ba8079e 100644 --- a/resources/pagedown_math.js +++ b/resources/pagedown_math.js @@ -1,13 +1,12 @@ function mathjax_pagedown($) { - if ('MathJax' in window) { - $.each(window.editors, function (id, editor) { - var preview = $('div.wmd-preview#' + id + '_wmd_preview')[0]; - editor.hooks.chain('onPreviewRefresh', function () { - MathJax.typeset(preview); - }); - MathJax.typeset(preview); + $.each(window.editors, function (id, editor) { + console.log(id); + var preview = $('div.wmd-preview#' + id + '_wmd_preview')[0]; + editor.hooks.chain('onPreviewRefresh', function () { + renderKatex(preview); }); - } + renderKatex(preview); + }); } window.mathjax_pagedown = mathjax_pagedown; diff --git a/templates/base.html b/templates/base.html index 1cd66d9..1158dc7 100644 --- a/templates/base.html +++ b/templates/base.html @@ -376,6 +376,8 @@ {% block extra_js %}{% endblock %} {% block bodyend %}{% endblock %} + + {% include "katex-load.html" %} {% block footer %}