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.

markdown: Don't double-process mentions formatted with a link.

PieterCKc752820dc09f
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py
index 3fb4567b135..cb64be5c094 100644
--- a/zerver/lib/markdown/__init__.py
+++ b/zerver/lib/markdown/__init__.py
@@ -44,7 +44,7 @@ from zerver.lib.exceptions import MarkdownRenderingError
from zerver.lib.markdown import fenced_code
from zerver.lib.markdown.fenced_code import FENCE_RE
from zerver.lib.mention import (
- BEFORE_MENTION_ALLOWED_REGEX,
+ BEFORE_LINK_PRODUCING_MENTION_ALLOWED_REGEX,
ChannelTopicInfo,
FullNameInfo,
MentionBackend,
@@ -158,7 +158,7 @@ def verbose_compile(pattern: str) -> Pattern[str]:
STREAM_LINK_REGEX = rf"""
- {BEFORE_MENTION_ALLOWED_REGEX} # Start after whitespace or specified chars
+ {BEFORE_LINK_PRODUCING_MENTION_ALLOWED_REGEX} # Start after whitespace or specified chars
\#\*\* # and after hash sign followed by double asterisks
(?P<stream_name>[^\*]+) # stream name can contain anything
\*\* # ends by double asterisks
@@ -179,7 +179,7 @@ def get_compiled_stream_link_regex() -> Pattern[str]:
STREAM_TOPIC_LINK_REGEX = rf"""
- {BEFORE_MENTION_ALLOWED_REGEX} # Start after whitespace or specified chars
+ {BEFORE_LINK_PRODUCING_MENTION_ALLOWED_REGEX} # Start after whitespace or specified chars
\#\*\* # and after hash sign followed by double asterisks
(?P<stream_name>[^\*>]+) # stream name can contain anything except >
> # > acts as separator
@@ -202,7 +202,7 @@ def get_compiled_stream_topic_link_regex() -> Pattern[str]:
STREAM_TOPIC_MESSAGE_LINK_REGEX = rf"""
- {BEFORE_MENTION_ALLOWED_REGEX} # Start after whitespace or specified chars
+ {BEFORE_LINK_PRODUCING_MENTION_ALLOWED_REGEX} # Start after whitespace or specified chars
\#\*\* # and after hash sign followed by double asterisks
(?P<stream_name>[^\*>]+) # stream name can contain anything except >
> # > acts as separator
diff --git a/zerver/lib/mention.py b/zerver/lib/mention.py
index a861ed1ef08..6c6c1b2b3ba 100644
--- a/zerver/lib/mention.py
+++ b/zerver/lib/mention.py
@@ -25,6 +25,10 @@ from zerver.models.users import is_cross_realm_bot_email
BEFORE_MENTION_ALLOWED_REGEX = r"(?<![^\s\'\"\(\{\[\/<])"
+# For mentions that generate a link of their own, don't match syntax
+# that is formatted with another link. e.g., "[#**channel**]({link})"
+BEFORE_LINK_PRODUCING_MENTION_ALLOWED_REGEX = r"(?<![^\s\'\"\(\{\/<])"
+
# Match multi-word string between @** ** or match any one-word
# sequences after @
MENTIONS_RE = re.compile(
diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py
index d007d2b6fc4..ba3b2fef5b7 100644
--- a/zerver/tests/test_markdown.py
+++ b/zerver/tests/test_markdown.py
@@ -1,5 +1,6 @@
import os
import re
+from dataclasses import dataclass
from html import escape
from textwrap import dedent
from typing import Any
@@ -764,6 +765,54 @@ class MarkdownLinkTest(ZulipTestCase):
'<p>To <a href="bitcoin:1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa">bitcoin:1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa</a> or not to bitcoin</p>',
)
+ def test_mentions_formatted_with_link(self) -> None:
+ # Make sure the inline link processor is takes priority over the mention processors
+ # and don't double-process mentions with a link.
+ sender_user_profile = self.example_user("othello")
+ msg = Message(
+ sender=sender_user_profile,
+ sending_client=get_client("test"),
+ realm=sender_user_profile.realm,
+ )
+
+ @dataclass
+ class MentionFixture:
+ name: str
+ mention_syntax: str
+ expected_html_class: str
+
+ mention_fixtures = [
+ MentionFixture(
+ name="channel_mention",
+ mention_syntax="#**Denmark**",
+ expected_html_class="stream",
+ ),
+ MentionFixture(
+ name="channel_topic_mention",
+ mention_syntax="#**Denmark>topic**",
+ expected_html_class="stream-topic",
+ ),
+ MentionFixture(
+ name="channel_topic_message_mention",
+ mention_syntax="#**Denmark>topic>@123**",
+ expected_html_class="message-link",
+ ),
+ ]
+
+ for fixture in mention_fixtures:
+ with self.subTest(fixture.name):
+ # Make sure the mention syntax is valid first.
+ self.assertIn(
+ fixture.expected_html_class,
+ render_message_markdown(msg, fixture.mention_syntax).rendered_content,
+ )
+ link = "https://chat.zulip.org"
+ mention_with_link = f"[{fixture.mention_syntax}]({link})"
+ self.assertEqual(
+ render_message_markdown(msg, mention_with_link).rendered_content,
+ f'<p><a href="{link}">{escape(fixture.mention_syntax)}</a></p>',
+ )
+
class MarkdownEmbedsTest(ZulipTestCase):
def assert_message_content_is(