email_notification: Fix bad rendering of math formulas.

Earlier, for the emails having latex math like
"$$d^* = +\infty$$", the bad rendering led to the math
being included multiple times in the email body.

This was due to displaying KaTeX HTML without the CSS.

This commit fixes the incorrect behavior by replacing
the KaTeX with the raw LaTex source.

Fixes part of #25289.
This commit is contained in:
Prakhar Pratyush
2023-11-20 20:34:05 +05:30
committed by Tim Abbott
parent c85c333c90
commit f7f5131aa8
2 changed files with 28 additions and 0 deletions

View File

@@ -248,6 +248,20 @@ def build_message_list(
relative_to_full_url(fragment, user.realm.uri)
fix_emojis(fragment, user.emojiset)
fix_spoilers_in_html(fragment, user.default_language)
# Selecting the <span> elements with class 'katex'
katex_spans = fragment.xpath("//span[@class='katex']")
# Iterate through 'katex_spans' and replace with a new <span> having LaTeX text.
for katex_span in katex_spans:
latex_text = katex_span.xpath(".//annotation[@encoding='application/x-tex']")[0].text
# We store 'tail' to insert them back as the replace operation removes it.
tail = katex_span.tail
latex_span = lxml.html.Element("span")
latex_span.text = f"$${latex_text}$$"
katex_span.getparent().replace(katex_span, latex_span)
latex_span.tail = tail
html = lxml.html.tostring(fragment, encoding="unicode")
if sender:
plain, html = prepend_sender_to_message(plain, html, sender)