From 81b669b36e4541b6f6f2101b71e5880643c350e8 Mon Sep 17 00:00:00 2001 From: nanobot-agent Date: Tue, 24 Feb 2026 12:44:17 +0000 Subject: [PATCH] fix(slack): post-process slackify_markdown output to catch leftover artifacts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The slackify_markdown library (markdown-it) fails to convert **bold** when the closing ** is immediately followed by non-space text (e.g. **Status:**OK). This is a very common LLM output pattern that results in raw ** showing up in Slack messages. Add _fixup_mrkdwn() post-processor that: - Converts leftover **bold** → *bold* (Slack mrkdwn) - Converts leftover ## headers → *bold* (safety net) - Fixes over-escaped & in bare URLs - Protects code fences and inline code from being mangled Co-authored-by: Cursor --- nanobot/channels/slack.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/nanobot/channels/slack.py b/nanobot/channels/slack.py index e8175a3..87ba020 100644 --- a/nanobot/channels/slack.py +++ b/nanobot/channels/slack.py @@ -230,7 +230,9 @@ class SlackChannel(BaseChannel): _TABLE_RE = re.compile(r"(?m)^\|.*\|$(?:\n\|[\s:|-]*\|$)(?:\n\|.*\|$)*") _CODE_FENCE_RE = re.compile(r"```[\s\S]*?```") + _INLINE_CODE_RE = re.compile(r"`[^`]+`") _LEFTOVER_BOLD_RE = re.compile(r"\*\*(.+?)\*\*") + _LEFTOVER_HEADER_RE = re.compile(r"^#{1,6}\s+(.+)$", re.MULTILINE) _BARE_URL_RE = re.compile(r"(? str: """Fix markdown artifacts that slackify_markdown misses. - Handles: leftover ``**bold**``, ``&`` in bare URLs, and - collapsed paragraph spacing. + The slackify_markdown library uses markdown-it which requires certain + boundary conditions for emphasis. Patterns like ``**key:**value`` + (closing ``**`` immediately followed by non-space) are not recognised + as bold and pass through as literal asterisks. This method catches + those leftovers, stray ``## headers``, and over-escaped ``&`` in + bare URLs, while leaving code spans untouched. """ # Protect code blocks from further processing code_blocks: list[str] = [] @@ -258,9 +264,10 @@ class SlackChannel(BaseChannel): return f"\x00CB{len(code_blocks) - 1}\x00" text = cls._CODE_FENCE_RE.sub(_save_code, text) + text = cls._INLINE_CODE_RE.sub(_save_code, text) - # Fix leftover **bold** the library didn't convert (e.g. **key:**val) text = cls._LEFTOVER_BOLD_RE.sub(r"*\1*", text) + text = cls._LEFTOVER_HEADER_RE.sub(r"*\1*", text) # Fix & in bare URLs that the library over-escaped text = cls._BARE_URL_RE.sub(