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: Stop attempting to expand/collapse re2 regex.

Alex Vandiver8c8dbb3d66a6
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py
index d352d79bfcf..53bbb32b16d 100644
--- a/zerver/lib/markdown/__init__.py
+++ b/zerver/lib/markdown/__init__.py
@@ -1821,25 +1821,7 @@ def prepare_linkifier_pattern(source: str) -> str:
# We use an extended definition of 'whitespace' which is
# equivalent to \p{White_Space} -- since \s in re2 only matches
# ASCII spaces, and re2 does not support \p{White_Space}.
- regex = rf"""
- (?P<{BEFORE_CAPTURE_GROUP}>
- ^ |
- \s | {next_line} | \pZ |
- ['"\(,:<]
- )
- (?P<{OUTER_CAPTURE_GROUP}>
- {source}
- )
- (?P<{AFTER_CAPTURE_GROUP}>
- $ |
- [^\pL\pN]
- )
- """
- # Strip out the spaces and newlines added to make the above
- # legible -- re2 does not have the equivalent of the /x modifier
- # that does this automatically. Note that we are careful to not
- # strip _whitespace_, which would strip the literal \u0085 out.
- return regex.replace(" ", "").replace("\n", "")
+ return rf"""(?P<{BEFORE_CAPTURE_GROUP}>^|\s|{next_line}|\pZ|['"\(,:<])(?P<{OUTER_CAPTURE_GROUP}>{source})(?P<{AFTER_CAPTURE_GROUP}>$|[^\pL\pN])"""
# Given a regular expression pattern, linkifies groups that match it
diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py
index 48b1a770a07..33c4eb93e14 100644
--- a/zerver/tests/test_markdown.py
+++ b/zerver/tests/test_markdown.py
@@ -1366,6 +1366,25 @@ class MarkdownTest(ZulipTestCase):
[{"url": "https://example.com/A%20Test/%25%25%ba/123", "text": "url-123"}],
)
+ # Test spaces in the linkifier pattern
+ RealmFilter(
+ realm=realm,
+ pattern=r"community guidelines",
+ url_template="https://zulip.com/development-community/#community-norms",
+ ).save()
+ converted = markdown_convert("community guidelines", message_realm=realm, message=msg)
+ self.assertEqual(
+ converted.rendered_content,
+ '<p><a href="https://zulip.com/development-community/#community-norms">community guidelines</a></p>',
+ )
+ converted = markdown_convert(
+ "please observe community guidelines here", message_realm=realm, message=msg
+ )
+ self.assertEqual(
+ converted.rendered_content,
+ '<p>please observe <a href="https://zulip.com/development-community/#community-norms">community guidelines</a> here</p>',
+ )
+
def test_multiple_matching_realm_patterns(self) -> None:
realm = get_realm("zulip")
self.check_add_linkifiers(