From 549dd8a4c4a4ba19e335bbd4362a7a1fde5e4cb2 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Tue, 14 Nov 2023 19:09:09 +0000 Subject: [PATCH] markdown: Expand prepare_linkifier_pattern to make it more legible. --- zerver/lib/markdown/__init__.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py index 0b0896261f..009abcf5b8 100644 --- a/zerver/lib/markdown/__init__.py +++ b/zerver/lib/markdown/__init__.py @@ -1812,7 +1812,24 @@ def prepare_linkifier_pattern(source: str) -> str: whitespace, or opening delimiters, won't match if there are word characters directly after, and saves what was matched as OUTER_CAPTURE_GROUP.""" - return rf"""(?P<{BEFORE_CAPTURE_GROUP}>^|\s|['"\(,:<])(?P<{OUTER_CAPTURE_GROUP}>{source})(?P<{AFTER_CAPTURE_GROUP}>$|[^\pL\pN])""" + regex = rf""" + (?P<{BEFORE_CAPTURE_GROUP}> + ^ | + \s | + ['"\(,:<] + ) + (?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. + return regex.replace(" ", "").replace("\n", "") # Given a regular expression pattern, linkifies groups that match it