From c3d200075dc2a4c019ee5547129de45e371d050d Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Thu, 24 Aug 2023 21:41:47 +0200 Subject: Fix regresion with option unfurl_auto_link_display This option wasn't accounted for when the message rendering was changed to using blocks in commit 74da303. --- wee_slack.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/wee_slack.py b/wee_slack.py index a699c42..f439d58 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -4772,11 +4772,12 @@ def unfurl_block_element(element): else: return element["image_url"] elif element["type"] == "link": - if element.get("text"): + text = element.get("text") + if text and text != element["url"]: if element.get("style", {}).get("code"): - return element["text"] + return text else: - return "{} ({})".format(element["url"], element["text"]) + return unfurl_link(element["url"], text) else: return element["url"] elif element["type"] == "emoji": @@ -4797,6 +4798,17 @@ def unfurl_block_element(element): ) +def unfurl_link(url, text): + match_url = r"^\w+:(//)?{}$".format(re.escape(text)) + url_matches_desc = re.match(match_url, url) + if url_matches_desc and config.unfurl_auto_link_display == "text": + return text + elif url_matches_desc and config.unfurl_auto_link_display == "url": + return url + else: + return "{} ({})".format(url, text) + + def unfurl_refs(text): """ input : <@U096Q7CQM|someuser> has joined the channel @@ -4827,14 +4839,7 @@ def unfurl_refs(text): elif ref.startswith("!date"): return fallback else: - match_url = r"^\w+:(//)?{}$".format(re.escape(fallback)) - url_matches_desc = re.match(match_url, ref) - if url_matches_desc and config.unfurl_auto_link_display == "text": - return fallback - elif url_matches_desc and config.unfurl_auto_link_display == "url": - return ref - else: - return "{} ({})".format(ref, fallback) + return unfurl_link(ref, fallback) return ref return re.sub(r"<([^|>]*)(?:\|([^>]*))?>", unfurl_ref, text) -- cgit