mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	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:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							c85c333c90
						
					
				
				
					commit
					f7f5131aa8
				
			@@ -248,6 +248,20 @@ def build_message_list(
 | 
				
			|||||||
        relative_to_full_url(fragment, user.realm.uri)
 | 
					        relative_to_full_url(fragment, user.realm.uri)
 | 
				
			||||||
        fix_emojis(fragment, user.emojiset)
 | 
					        fix_emojis(fragment, user.emojiset)
 | 
				
			||||||
        fix_spoilers_in_html(fragment, user.default_language)
 | 
					        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")
 | 
					        html = lxml.html.tostring(fragment, encoding="unicode")
 | 
				
			||||||
        if sender:
 | 
					        if sender:
 | 
				
			||||||
            plain, html = prepend_sender_to_message(plain, html, sender)
 | 
					            plain, html = prepend_sender_to_message(plain, html, sender)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1593,6 +1593,20 @@ class TestMessageNotificationEmails(ZulipTestCase):
 | 
				
			|||||||
        )
 | 
					        )
 | 
				
			||||||
        self.assertEqual(actual_output, expected_output)
 | 
					        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:
 | 
					    def test_empty_backticks_in_missed_message(self) -> None:
 | 
				
			||||||
        msg_id = self.send_personal_message(
 | 
					        msg_id = self.send_personal_message(
 | 
				
			||||||
            self.example_user("othello"),
 | 
					            self.example_user("othello"),
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user