Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

email_notification: Fix bad rendering of math formulas.

Prakhar Pratyushf7f5131aa871
diff --git a/zerver/lib/email_notifications.py b/zerver/lib/email_notifications.py
index ef8eb6a99e7..c74bdab6e6d 100644
--- a/zerver/lib/email_notifications.py
+++ b/zerver/lib/email_notifications.py
@@ -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)
diff --git a/zerver/tests/test_message_notification_emails.py b/zerver/tests/test_message_notification_emails.py
index 77a18298a04..dd230d23ddc 100644
--- a/zerver/tests/test_message_notification_emails.py
+++ b/zerver/tests/test_message_notification_emails.py
@@ -1593,6 +1593,20 @@ class TestMessageNotificationEmails(ZulipTestCase):
)
self.assertEqual(actual_output, expected_output)
+ def test_latex_math_formulas_in_email(self) -> None:
+ msg_id = self.send_stream_message(
+ self.example_user("iago"), "Denmark", "Equation: $$d^* = +\\infty$$ is correct."
+ )
+ verify_body_include = ["Equation: <span>$$d^* = +\\infty$$</span> is correct"]
+ email_subject = "#Denmark > test"
+ self._test_cases(
+ msg_id,
+ verify_body_include,
+ email_subject,
+ verify_html_body=True,
+ trigger=NotificationTriggers.STREAM_EMAIL,
+ )
+
def test_empty_backticks_in_missed_message(self) -> None:
msg_id = self.send_personal_message(
self.example_user("othello"),