From 732ad89f3d7b13664d7ccc09490b63bfa6c8392e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Oliva?= Date: Fri, 1 Sep 2023 15:55:49 -0600 Subject: [PATCH] markdown: Fix URL link topic skipping query. When searching for links inside a topic name, the question mark (?) was used to split the topic. If a URL had a query after the URL (e.g., "?foo=bar"), then the query was trimmed from the URL. Removing the question mark from `basic_link_splitter` is sufficient to fix this issue. The `get_web_link_regex` function then removes the trailing punctuation if any, including literal question marks. Fixes #26368. --- zerver/lib/markdown/__init__.py | 2 +- zerver/tests/test_markdown.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py index 21b6f9ac7c..4808511222 100644 --- a/zerver/lib/markdown/__init__.py +++ b/zerver/lib/markdown/__init__.py @@ -2284,7 +2284,7 @@ def make_md_engine(linkifiers_key: int, email_gateway: bool) -> None: # Split the topic name into multiple sections so that we can easily use # our common single link matching regex on it. -basic_link_splitter = re.compile(r"[ !;\?\),\'\"]") +basic_link_splitter = re.compile(r"[ !;\),\'\"]") def percent_escape_format_string(format_string: str) -> str: diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py index 979a29f56b..c75f129c50 100644 --- a/zerver/tests/test_markdown.py +++ b/zerver/tests/test_markdown.py @@ -1160,6 +1160,36 @@ class MarkdownTest(ZulipTestCase): ], ) + # Query strings in a URL should be included in the link. + msg.set_topic_name("https://google.com/test?foo=bar") + converted_topic = topic_links(realm.id, msg.topic_name()) + self.assertEqual( + converted_topic, + [ + { + "url": "https://google.com/test?foo=bar", + "text": "https://google.com/test?foo=bar", + }, + ], + ) + # But question marks at the end of sentence are not part of the URL. + msg.set_topic_name("Have you seen github.com/zulip?") + converted_topic = topic_links(realm.id, msg.topic_name()) + self.assertEqual( + converted_topic, + [ + {"url": "https://github.com/zulip", "text": "github.com/zulip"}, + ], + ) + msg.set_topic_name("Do you like https://example.com? I love it.") + converted_topic = topic_links(realm.id, msg.topic_name()) + self.assertEqual( + converted_topic, + [ + {"url": "https://example.com", "text": "https://example.com"}, + ], + ) + def check_add_linkifiers( self, linkifiers: List[RealmFilter], expected_linkifier_reprs: List[str] ) -> None: