Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown: Inline Twitter previews instead of appending at end.
Rohitt Vashishtha87e01cd1fa76
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.pyindex b36e2b0d473..ae4fcad8a8e 100644--- a/zerver/lib/markdown/__init__.py+++ b/zerver/lib/markdown/__init__.py@@ -1054,6 +1054,23 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):if info['remove'] is not None:info['parent'].remove(info['remove'])+ def handle_tweet_inlining(+ self,+ root: Element,+ found_url: ResultWithFamily[Tuple[str, Optional[str]]],+ twitter_data: Element,+ ) -> None:+ info = self.get_inlining_information(root, found_url)++ if info['index'] is not None:+ div = Element("div")+ root.insert(info['index'], div)+ else:+ div = SubElement(root, "div")++ div.set("class", "inline-preview-twitter")+ div.insert(0, twitter_data)+def find_proper_insertion_index(self, grandparent: Element, parent: Element,parent_index_in_grandparent: int) -> int:# If there are several inline images from same paragraph, ensure that@@ -1069,7 +1086,7 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):return insertion_indexuncle = grandparent[insertion_index]- inline_image_classes = ['message_inline_image', 'message_inline_ref']+ inline_image_classes = ['message_inline_image', 'message_inline_ref', 'inline-preview-twitter']if (uncle.tag != 'div' or'class' not in uncle.keys() or@@ -1173,9 +1190,7 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):# This link is not actually a tweet known to twittercontinuerendered_tweet_count += 1- div = SubElement(root, "div")- div.set("class", "inline-preview-twitter")- div.insert(0, twitter_data)+ self.handle_tweet_inlining(root, found_url, twitter_data)continueyoutube = self.youtube_image(url)if youtube is not None:diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.pyindex 03ecfc1eca3..f8ae6bbd5f5 100644--- a/zerver/tests/test_markdown.py+++ b/zerver/tests/test_markdown.py@@ -837,6 +837,17 @@ class MarkdownTest(ZulipTestCase):make_inline_twitter_preview('http://twitter.com/wdaher/status/287977969287315457', normal_tweet_html),make_inline_twitter_preview('https://twitter.com/wdaher/status/287977969287315456', normal_tweet_html)))+ # Test smart in-place inlining behavior:+ msg = ('Paragraph 1: http://twitter.com/wdaher/status/287977969287315456\n\n'+ 'Paragraph 2\n\n'+ 'Paragraph 3: http://twitter.com/wdaher/status/287977969287315457')+ converted = markdown_convert_wrapper(msg)+ self.assertEqual(converted, '<p>Paragraph 1: {}</p>\n{}<p>Paragraph 2</p>\n<p>Paragraph 3: {}</p>\n{}'.format(+ make_link('http://twitter.com/wdaher/status/287977969287315456'),+ make_inline_twitter_preview('http://twitter.com/wdaher/status/287977969287315456', normal_tweet_html),+ make_link('http://twitter.com/wdaher/status/287977969287315457'),+ make_inline_twitter_preview('http://twitter.com/wdaher/status/287977969287315457', normal_tweet_html)))+# Tweet has a mention in a URL, only the URL is linkedmsg = 'http://twitter.com/wdaher/status/287977969287315458'