slack_import: Create and use the convert_slack_workspace_mentions helper.

This prep commit extracts the logic for reformatting Slack mentions into
Zulip mentions from `convert_to_zulip_markdown` into a new helper
function, `convert_slack_workspace_mentions`.

This is done to make the logic reformatting logic be reusable else where
such as in the Slack webhook or Slack incoming webhook.
This commit is contained in:
PieterCK 2024-11-21 15:15:33 +07:00
parent 9e3cd756b0
commit c5bd16ec77
1 changed files with 12 additions and 7 deletions

View File

@ -151,6 +151,17 @@ def convert_markdown_syntax(text: str, regex: str, zulip_keyword: str) -> str:
return text
def convert_slack_workspace_mentions(text: str) -> str:
# Map Slack's mention all: '<!everyone>' to '@**all** '
# Map Slack's mention all: '<!channel>' to '@**all** '
# Map Slack's mention all: '<!here>' to '@**all** '
# No regex for this as it can be present anywhere in the sentence
text = text.replace("<!everyone>", "@**all**")
text = text.replace("<!channel>", "@**all**")
text = text.replace("<!here>", "@**all**")
return text
# Markdown mapping
def convert_to_zulip_markdown(
text: str,
@ -163,13 +174,7 @@ def convert_to_zulip_markdown(
text = convert_markdown_syntax(text, SLACK_STRIKETHROUGH_REGEX, "~~")
text = convert_markdown_syntax(text, SLACK_ITALIC_REGEX, "*")
# Map Slack's mention all: '<!everyone>' to '@**all** '
# Map Slack's mention all: '<!channel>' to '@**all** '
# Map Slack's mention all: '<!here>' to '@**all** '
# No regex for this as it can be present anywhere in the sentence
text = text.replace("<!everyone>", "@**all**")
text = text.replace("<!channel>", "@**all**")
text = text.replace("<!here>", "@**all**")
text = convert_slack_workspace_mentions(text)
# Map Slack channel mention: '<#C5Z73A7RA|general>' to '#**general**'
for cname, ids in added_channels.items():