slack_data_import: Fix incorrect hyperlink conversion.

Currently, Slack messages containing hyperlinks
(e.g.,<http://foo.com|Foo!>) are converted like
normal links. This commit reformats Slack
hyperlinks into Zulip-friendly markdown
(e.g., [Foo!](http://foo.com)).

Part of #32165.
This commit is contained in:
PieterCK 2024-08-12 12:25:40 +07:00 committed by Tim Abbott
parent 18a8125dac
commit fc50736f4e
3 changed files with 21 additions and 4 deletions

View File

@ -157,13 +157,20 @@ def convert_markdown_syntax(text: str, regex: str, zulip_keyword: str) -> str:
def convert_link_format(text: str) -> tuple[str, bool]: def convert_link_format(text: str) -> tuple[str, bool]:
""" """
1. Converts '<https://foo.com>' to 'https://foo.com' 1. Converts '<https://foo.com>' to 'https://foo.com'
2. Converts '<https://foo.com|foo>' to 'https://foo.com|foo' 2. Converts '<https://foo.com|foo>' to '[foo](https://foo.com)'
""" """
has_link = False has_link = False
for match in re.finditer(LINK_REGEX, text, re.VERBOSE): for match in re.finditer(LINK_REGEX, text, re.VERBOSE):
converted_text = match.group(0).replace(">", "").replace("<", "") slack_url = match.group(0)
url_parts = slack_url[1:-1].split("|", maxsplit=1)
# Check if there's a pipe with text after it
if len(url_parts) == 2:
converted_url = f"[{url_parts[1]}]({url_parts[0]})"
else:
converted_url = url_parts[0]
has_link = True has_link = True
text = text.replace(match.group(0), converted_text) text = text.replace(slack_url, converted_url)
return text, has_link return text, has_link

View File

@ -8,7 +8,12 @@
{ {
"name": "slack_link_with_pipe", "name": "slack_link_with_pipe",
"input": ">Google logo today:\n><https://foo.com|foo>\n>Kinda boring", "input": ">Google logo today:\n><https://foo.com|foo>\n>Kinda boring",
"conversion_output": ">Google logo today:\n>https://foo.com|foo\n>Kinda boring" "conversion_output": ">Google logo today:\n>[foo](https://foo.com)\n>Kinda boring"
},
{
"name": "slack_link_with_pipes",
"input": ">Google logo today:\n><https://foo.com|foo|oof>\n>Kinda boring",
"conversion_output": ">Google logo today:\n>[foo|oof](https://foo.com)\n>Kinda boring"
}, },
{ {
"name": "slack_link2", "name": "slack_link2",

View File

@ -113,6 +113,11 @@ class SlackMessageConversion(ZulipTestCase):
self.assertEqual(text, "http://journals.plos.org/plosone/article") self.assertEqual(text, "http://journals.plos.org/plosone/article")
self.assertEqual(has_link, True) self.assertEqual(has_link, True)
message = "<http://chat.zulip.org/help/logging-in|Help logging in to CZO>"
text, mentioned_users, has_link = convert_to_zulip_markdown(message, [], {}, slack_user_map)
self.assertEqual(text, "[Help logging in to CZO](http://chat.zulip.org/help/logging-in)")
self.assertEqual(has_link, True)
message = "<mailto:foo@foo.com>" message = "<mailto:foo@foo.com>"
text, mentioned_users, has_link = convert_to_zulip_markdown(message, [], {}, slack_user_map) text, mentioned_users, has_link = convert_to_zulip_markdown(message, [], {}, slack_user_map)
self.assertEqual(text, "mailto:foo@foo.com") self.assertEqual(text, "mailto:foo@foo.com")