From ff45665bbc38703686e0a35451df80abca4cf736 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Thu, 10 Nov 2022 15:22:17 -0600 Subject: [PATCH] Make markdown latex better --- judge/jinja2/markdown/__init__.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/judge/jinja2/markdown/__init__.py b/judge/jinja2/markdown/__init__.py index e34f74a..d54f103 100644 --- a/judge/jinja2/markdown/__init__.py +++ b/judge/jinja2/markdown/__init__.py @@ -7,6 +7,7 @@ from pymdownx import superfences EXTENSIONS = [ + "pymdownx.arithmatex", "pymdownx.magiclink", "pymdownx.betterem", "pymdownx.details", @@ -76,11 +77,29 @@ def markdown(value, lazy_load=False): html = _markdown.markdown( 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: - soup = BeautifulSoup(html, features="lxml") + if lazy_load or True: + soup = BeautifulSoup(html, features="html.parser") for img in soup.findAll("img"): if img.get("src"): img["data-src"] = img["src"] @@ -90,5 +109,4 @@ def markdown(value, lazy_load=False): img["data-src"] = img["src"] img["src"] = "" html = str(soup) - return '
%s
' % html