:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(tasks): improve Slack mrkdwn output for relay messages (#61645)
Vojta Bartoš68267a9cb764
diff --git a/products/tasks/backend/temporal/slack_relay/activities.py b/products/tasks/backend/temporal/slack_relay/activities.pyindex 822730eeed11..e414d963bf46 100644--- a/products/tasks/backend/temporal/slack_relay/activities.py+++ b/products/tasks/backend/temporal/slack_relay/activities.py@@ -2,101 +2,115 @@ import refrom dataclasses import dataclassfrom typing import Any+from markdown_to_mrkdwn import SlackMarkdownConverterfrom temporalio import activityfrom posthog.temporal.common.logger import get_loggerlogger = get_logger(__name__)+_CONVERTER = SlackMarkdownConverter()++_RE_TABLE_ROW = re.compile(r"^\s*\|.*\|\s*$")+_RE_TABLE_SEPARATOR_CELL = re.compile(r"^:?-{2,}:?$")+_RE_FENCE = re.compile(r"^\s*(```|~~~)")+_RE_INLINE_MARKDOWN_MARKERS = re.compile(r"\*\*|__|\*|_|~~|`")+_RE_MD_LINK = re.compile(r"\[([^\]]+)\]\(([^)]+)\)")+class _RelayAlreadyRecorded(Exception):"""Raised when a relay was already recorded while holding the row lock."""def _markdown_to_slack_mrkdwn(text: str) -> str:- """Convert markdown to Slack mrkdwn format.+ """Convert markdown to Slack ``mrkdwn`` via ``markdown_to_mrkdwn``.- Handles the most common differences while preserving code blocks.+ Tables are pre-converted to fenced code blocks before the library runs because+ Slack ``mrkdwn`` is rendered in a proportional font — pipe-separated rows do+ not line up. A fenced code block forces monospace and the columns align."""- # Preserve code blocks from transformation- code_blocks: list[str] = []-- def _stash_code_block(match: re.Match) -> str:- code_blocks.append(match.group(0))- return f"\x00CODE{len(code_blocks) - 1}\x00"-- text = re.sub(r"```[\s\S]*?```", _stash_code_block, text)- text = re.sub(r"`[^`]+`", _stash_code_block, text)-- # Markdown tables → plain text columns- text = _convert_tables(text)-- # Headers → bold (### Header → *Header*)- text = re.sub(r"^#{1,6}\s+(.+)$", r"*\1*", text, flags=re.MULTILINE)-- # Bold: **text** → *text* (but not inside already-converted bold)- text = re.sub(r"\*\*(.+?)\*\*", r"*\1*", text)-- # Strikethrough: ~~text~~ → ~text~- text = re.sub(r"~~(.+?)~~", r"~\1~", text)-- # Images before links since ![...] is more specific than [...]- text = re.sub(r"!\[([^\]]*)\]\(([^)]+)\)", r"<\2|\1>", text)-- # Links: [text](url) → <url|text>- text = re.sub(r"\[([^\]]+)\]\(([^)]+)\)", r"<\2|\1>", text)-- # Restore code blocks- for i, block in enumerate(code_blocks):- text = text.replace(f"\x00CODE{i}\x00", block)+ if not text:+ return text+ return _CONVERTER.convert(_tables_to_fenced_code_blocks(text))- return text+def _tables_to_fenced_code_blocks(text: str) -> str:+ """Replace pipe-syntax markdown tables with fenced code blocks of padded columns.-def _convert_tables(text: str) -> str:- """Convert markdown tables to aligned plain-text columns."""+ A run of consecutive ``|…|`` lines surrounding a ``---`` separator row is+ treated as a table; runs without a separator are left untouched. Lines inside+ an existing fenced code block are skipped entirely so we don't mis-detect a+ pipe-shaped line of source code as a table.+ """lines = text.split("\n")- result: list[str] = []- table_lines: list[str] = []+ out: list[str] = []+ run: list[str] = []+ in_fence = False- def _flush_table() -> None:- if not table_lines:+ def _flush() -> None:+ if not run:return- rows: list[list[str]] = []- for line in table_lines:- cells = [c.strip() for c in line.strip().strip("|").split("|")]- # Skip separator rows (----, :---:, etc.)- if all(re.match(r"^:?-+:?$", c) for c in cells):- continue- rows.append(cells)- if not rows:- table_lines.clear()- return- # Calculate column widths- col_count = max(len(r) for r in rows)- widths = [0] * col_count- for row in rows:- for i, cell in enumerate(row):- if i < col_count:- widths[i] = max(widths[i], len(cell))- for row in rows:- padded = []- for i in range(col_count):- cell = row[i] if i < len(row) else ""- padded.append(cell.ljust(widths[i]))- result.append(" ".join(padded).rstrip())- table_lines.clear()+ rendered = _render_table(run)+ out.extend(run if rendered is None else [rendered])+ run.clear()for line in lines:- stripped = line.strip()- if re.match(r"^\|.*\|$", stripped):- table_lines.append(stripped)+ if _RE_FENCE.match(line):+ _flush()+ in_fence = not in_fence+ out.append(line)+ continue+ if not in_fence and _RE_TABLE_ROW.match(line):+ run.append(line)else:- _flush_table()- result.append(line)- _flush_table()+ _flush()+ out.append(line)+ _flush()++ return "\n".join(out)- return "\n".join(result)++def _render_table(rows_raw: list[str]) -> str | None:+ """Render a candidate table block to a fenced code block, or ``None`` if invalid.++ A separator row of all-dashes cells (``---``, ``:---:``) is required — without+ it the pipes are likely incidental rather than a table.+ """+ parsed: list[list[str]] = []+ has_separator = False+ for line in rows_raw:+ cells = [c.strip() for c in line.strip().strip("|").split("|")]+ if cells and all(_RE_TABLE_SEPARATOR_CELL.match(c) for c in cells):+ has_separator = True+ continue+ parsed.append([_strip_inline_markdown(c) for c in cells])++ if not has_separator or not parsed:+ return None++ col_count = max(len(r) for r in parsed)+ widths = [0] * col_count+ for row in parsed:+ for i, cell in enumerate(row):+ widths[i] = max(widths[i], len(cell))++ body_lines = [+ " ".join((row[i] if i < len(row) else "").ljust(widths[i]) for i in range(col_count)).rstrip()+ for row in parsed+ ]+ return "```\n" + "\n".join(body_lines) + "\n```"+++def _strip_inline_markdown(cell: str) -> str:+ """Strip emphasis markers and unwrap links inside a cell.++ The cell ends up inside a fenced code block where ``*bold*`` and ``[text](url)``+ are not interpreted, so leaving the markers in just shifts column widths and+ adds visual noise.+ """+ cell = _RE_MD_LINK.sub(r"\1", cell)+ cell = _RE_INLINE_MARKDOWN_MARKERS.sub("", cell)+ return cell.strip()@dataclassdiff --git a/products/tasks/backend/temporal/slack_relay/tests/test_activities.py b/products/tasks/backend/temporal/slack_relay/tests/test_activities.pyindex 3bf65cca2189..4e55b2ea17e1 100644--- a/products/tasks/backend/temporal/slack_relay/tests/test_activities.py+++ b/products/tasks/backend/temporal/slack_relay/tests/test_activities.py@@ -1,5 +1,6 @@from typing import ClassVar+import unittestfrom unittest.mock import patchfrom django.test import TestCase@@ -105,36 +106,51 @@ class TestRelaySlackMessage(TestCase):assert relay_id in self.task_run.state.get("slack_sent_relay_ids", [])-class TestMarkdownToSlackMrkdwn(TestCase):+class TestMarkdownToSlackMrkdwn(unittest.TestCase):@parameterized.expand([("bold", "**hello**", "*hello*"),- ("nested_bold_in_list", "- **MIT** is permissive", "- *MIT* is permissive"),+ ("italic_asterisk", "*italic*", "_italic_"),+ ("italic_underscore", "_italic_", "_italic_"),+ ("bold_italic", "***boldit***", "*_boldit_*"),("strikethrough", "~~removed~~", "~removed~"),("link", "[Click here](https://example.com)", "<https://example.com|Click here>"),- ("image", "", "<https://img.png|alt>"),("h1", "# Title", "*Title*"),("h3", "### Section", "*Section*"),- ("inline_code_preserved", "Use `git commit`", "Use `git commit`"),- ("bold_not_in_code", "**bold** and `**not bold**`", "*bold* and `**not bold**`"),+ ("dash_bullets", "- one\n- two", "• one\n• two"),+ ("ordered_list_preserved", "1. one\n2. two", "1. one\n2. two"),+ ("task_list", "- [ ] todo\n- [x] done", "• ☐ todo\n• ☑ done"),+ ("horizontal_rule", "---", "──────────"),+ ("blockquote_preserved", "> quote", "> quote"),+ ("nested_bold_in_dash_list", "- **MIT** is permissive", "• *MIT* is permissive"),("plain_text_unchanged", "Hello world", "Hello world"),+ ("inline_code_preserved", "Use `git commit`", "Use `git commit`"),])def test_inline_conversions(self, _name, markdown, expected):assert _markdown_to_slack_mrkdwn(markdown) == expected- def test_table_converted_to_columns(self):- md = "| License | Key Points |\n|---|---|\n| **MIT** | Permissive |\n| **GPL** | Copyleft |"+ def test_empty_string_returns_unchanged(self):+ assert _markdown_to_slack_mrkdwn("") == ""++ def test_table_renders_as_fenced_code_block_with_aligned_columns(self):+ md = "| License | Key Points |\n|---|---|\n| MIT | Permissive |\n| GPL | Copyleft |"+ # Widest cells per column: 'License' (7) and 'Key Points' (10). Two-space gutter.+ # Trailing whitespace is rstripped, so the GPL row's narrower last cell isn't padded.+ expected = "```\nLicense Key Points\nMIT Permissive\nGPL Copyleft\n```"+ assert _markdown_to_slack_mrkdwn(md) == expected++ def test_table_strips_inline_markdown_from_cells(self):+ md = "| Name | Note |\n|---|---|\n| **MIT** | [docs](https://x.com) |"result = _markdown_to_slack_mrkdwn(md)- assert "---" not in result- assert "*MIT*" in result- assert "*GPL*" in result- assert "Permissive" in result- lines = [line for line in result.split("\n") if line.strip()]- assert len(lines) == 3 # header + 2 data rows-- def test_code_block_preserved(self):- md = "```python\n**not bold**\n```\nBut **this is bold**"+ # Bold markers and link syntax don't render inside a code block, so we strip them.+ assert "**" not in result+ assert "MIT" in result+ assert "docs" in result+ assert "https://x.com" not in result++ def test_pipe_rows_without_separator_are_not_treated_as_a_table(self):+ # No separator row → likely incidental pipes, not a table. Leave alone.+ md = "| a | b |\n| c | d |"result = _markdown_to_slack_mrkdwn(md)- assert "```python\n**not bold**\n```" in result- assert "*this is bold*" in result+ assert "```" not in result