-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat_converter.py
More file actions
310 lines (242 loc) · 11.6 KB
/
format_converter.py
File metadata and controls
310 lines (242 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""Slack-specific format conversion using AST-based parsing.
Port of markdown.ts from the Vercel Chat SDK Slack adapter.
Slack uses "mrkdwn" format which is similar but not identical to markdown:
- Bold: *text* (not **text**)
- Italic: _text_ (same)
- Strikethrough: ~text~ (not ~~text~~)
- Links: <url|text> (not [text](url))
- User mentions: <@U123>
- Channel mentions: <#C123|name>
"""
from __future__ import annotations
import re
from typing import Any
from chat_sdk.adapters.slack.cards import SlackBlock
from chat_sdk.shared.base_format_converter import (
BaseFormatConverter,
Content,
Root,
parse_markdown,
table_to_ascii,
)
# Match bare @mentions (e.g. "@george") to rewrite as Slack's `<@george>`.
# The lookbehind excludes `<` (already-formatted mentions like `<@U123>`) and
# any word character, so email addresses like `user@example.com` and
# `<mailto:foo@bar.com>` links pass through unchanged.
_BARE_MENTION_REGEX = re.compile(r"(?<![<\w])@(\w+)")
class SlackFormatConverter(BaseFormatConverter):
"""Convert between Slack mrkdwn and standard markdown / plain text."""
# -------------------------------------------------------------------------
# Core AST methods (required by BaseFormatConverter)
# -------------------------------------------------------------------------
def from_ast(self, ast: Root) -> str:
"""Render an AST to Slack mrkdwn format."""
return self._from_ast_with_node_converter(ast, self._node_to_mrkdwn)
def to_ast(self, platform_text: str) -> Root:
"""Parse Slack mrkdwn into an AST.
Converts Slack-specific syntax to standard markdown, then parses
with the shared parser.
"""
markdown = platform_text
# User mentions: <@U123|name> -> @name or <@U123> -> @U123
markdown = re.sub(r"<@([A-Z0-9_]+)\|([^<>]+)>", r"@\2", markdown)
markdown = re.sub(r"<@([A-Z0-9_]+)>", r"@\1", markdown)
# Channel mentions: <#C123|name> -> #name
markdown = re.sub(r"<#[A-Z0-9_]+\|([^<>]+)>", r"#\1", markdown)
markdown = re.sub(r"<#([A-Z0-9_]+)>", r"#\1", markdown)
# Links: <url|text> -> [text](url)
markdown = re.sub(r"<(https?://[^|<>]+)\|([^<>]+)>", r"[\2](\1)", markdown)
# Bare links: <url> -> url
markdown = re.sub(r"<(https?://[^<>]+)>", r"\1", markdown)
# Bold: *text* -> **text** (careful with emphasis)
markdown = re.sub(r"(?<![_*\\])\*([^*\n]+)\*(?![_*])", r"**\1**", markdown)
# Strikethrough: ~text~ -> ~~text~~
markdown = re.sub(r"(?<!~)~([^~\n]+)~(?!~)", r"~~\1~~", markdown)
return parse_markdown(markdown)
# -------------------------------------------------------------------------
# Overrides
# -------------------------------------------------------------------------
def render_postable(self, message: Any) -> str:
"""Render a postable message to Slack mrkdwn string.
Supports str, ``{"raw": ...}``, ``{"markdown": ...}``, ``{"ast": ...}``,
and card types (``{"card": ...}`` / ``CardElement``).
"""
if isinstance(message, str):
return self._convert_mentions_to_slack(message)
if hasattr(message, "raw"):
return self._convert_mentions_to_slack(message.raw)
if isinstance(message, dict):
if "raw" in message:
return self._convert_mentions_to_slack(message["raw"])
if "markdown" in message:
return self.from_markdown(message["markdown"])
if "ast" in message:
return self.from_ast(message["ast"])
if "card" in message:
from chat_sdk.cards import card_to_fallback_text
return card_to_fallback_text(message["card"])
if message.get("type") == "card":
from chat_sdk.cards import is_card_element
if is_card_element(message):
from chat_sdk.cards import card_to_fallback_text
return card_to_fallback_text(message) # type: ignore[arg-type]
return str(message)
# Dataclass-style objects
if hasattr(message, "markdown"):
return self.from_markdown(message.markdown)
if hasattr(message, "ast"):
return self.from_ast(message.ast)
if hasattr(message, "card"):
from chat_sdk.cards import card_to_fallback_text
return card_to_fallback_text(message.card)
return str(message)
def extract_plain_text(self, platform_text: str) -> str:
"""Extract plain text from Slack mrkdwn by stripping formatting."""
text = platform_text
# Remove user mentions formatting: <@U123|name> -> @name, <@U123> -> @U123
text = re.sub(r"<@([A-Z0-9_]+)\|([^<>]+)>", r"@\2", text)
text = re.sub(r"<@([A-Z0-9_]+)>", r"@\1", text)
# Remove channel mentions: <#C123|name> -> #name
text = re.sub(r"<#[A-Z0-9_]+\|([^<>]+)>", r"#\1", text)
text = re.sub(r"<#([A-Z0-9_]+)>", r"#\1", text)
# Remove links formatting: <url|text> -> text, <url> -> url
text = re.sub(r"<(https?://[^|<>]+)\|([^<>]+)>", r"\2", text)
text = re.sub(r"<(https?://[^<>]+)>", r"\1", text)
# Remove bold/italic/strikethrough markers
text = re.sub(r"\*([^*]+)\*", r"\1", text)
text = re.sub(r"_([^_]+)_", r"\1", text)
text = re.sub(r"~([^~]+)~", r"\1", text)
return text
# -------------------------------------------------------------------------
# Slack table block support
# -------------------------------------------------------------------------
def to_blocks_with_table(self, ast: Root) -> list[SlackBlock] | None:
"""Convert AST to Slack blocks, using native table block for the first table.
Returns None if the AST contains no tables (caller should use regular text).
Slack allows at most one table block per message; additional tables use ASCII.
"""
if not isinstance(ast, dict):
return None
children = ast.get("children", [])
has_table = any(isinstance(node, dict) and node.get("type") == "table" for node in children)
if not has_table:
return None
blocks: list[SlackBlock] = []
used_native_table = False
text_buffer: list[str] = []
def flush_text() -> None:
nonlocal text_buffer
if text_buffer:
text = "\n\n".join(text_buffer)
if text.strip():
blocks.append(
{
"type": "section",
"text": {"type": "mrkdwn", "text": text},
}
)
text_buffer = []
for child in children:
node = child if isinstance(child, dict) else {}
if node.get("type") == "table":
flush_text()
if used_native_table:
# Additional tables fall back to ASCII in a code block
ascii_table = table_to_ascii(node)
blocks.append(
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"```\n{ascii_table}\n```",
},
}
)
else:
blocks.append(self._mdast_table_to_slack_block(node))
used_native_table = True
else:
text_buffer.append(self._node_to_mrkdwn(node))
flush_text()
return blocks
# -------------------------------------------------------------------------
# Private helpers
# -------------------------------------------------------------------------
def _convert_mentions_to_slack(self, text: str) -> str:
"""Convert @mentions to Slack format: @name -> <@name>."""
return _BARE_MENTION_REGEX.sub(r"<@\1>", text)
def _node_to_mrkdwn(self, node: Content) -> str:
"""Convert a single AST node to Slack mrkdwn."""
if not isinstance(node, dict):
return str(node) if node else ""
node_type = node.get("type", "")
children = node.get("children", [])
if node_type == "paragraph":
return "".join(self._node_to_mrkdwn(c) for c in children)
if node_type == "text":
value = node.get("value", "")
return _BARE_MENTION_REGEX.sub(r"<@\1>", value)
if node_type == "strong":
content = "".join(self._node_to_mrkdwn(c) for c in children)
return f"*{content}*"
if node_type == "emphasis":
content = "".join(self._node_to_mrkdwn(c) for c in children)
return f"_{content}_"
if node_type == "delete":
content = "".join(self._node_to_mrkdwn(c) for c in children)
return f"~{content}~"
if node_type == "inlineCode":
return f"`{node.get('value', '')}`"
if node_type == "code":
lang = node.get("lang", "")
return f"```{lang}\n{node.get('value', '')}\n```"
if node_type == "link":
link_text = "".join(self._node_to_mrkdwn(c) for c in children)
return f"<{node.get('url', '')}|{link_text}>"
if node_type == "heading":
# Intentional improvement over TS SDK: Slack mrkdwn has no heading
# syntax, so we wrap headings in bold (*...*) for visual emphasis.
content = "".join(self._node_to_mrkdwn(c) for c in children)
return f"*{content}*"
if node_type == "blockquote":
return "\n".join(f"> {self._node_to_mrkdwn(c)}" for c in children)
if node_type == "list":
return self._render_list(node, 0, self._node_to_mrkdwn, "\u2022")
if node_type == "break":
return "\n"
if node_type == "thematicBreak":
return "---"
if node_type == "table":
return f"```\n{table_to_ascii(node)}\n```"
if node_type == "image":
url = node.get("url", "")
alt = node.get("alt", "")
if alt:
return f"{alt} ({url})"
return url
# Default fallback for any node with children
return self._default_node_to_text(node, self._node_to_mrkdwn)
def _mdast_table_to_slack_block(self, node: Content) -> SlackBlock:
"""Convert a table AST node to a Slack table block.
@see https://docs.slack.dev/reference/block-kit/blocks/table-block/
"""
rows_data: list[list[dict[str, str]]] = []
for row in node.get("children", []):
cells = []
for cell in row.get("children", []):
# Convert cell children to text, defaulting to a space if empty.
# Slack API requires table cell text to be at least 1 character.
# Use an explicit length check rather than a truthiness check to
# avoid substituting valid strings like "0".
raw_text = "".join(self._node_to_mrkdwn(c) for c in cell.get("children", []))
text = raw_text if len(raw_text) > 0 else " "
cells.append({"type": "raw_text", "text": text})
rows_data.append(cells)
block: SlackBlock = {"type": "table", "rows": rows_data}
align = node.get("align")
if align:
column_settings = [{"align": a or "left"} for a in align]
block["column_settings"] = column_settings
return block
# Backwards compatibility alias
SlackMarkdownConverter = SlackFormatConverter