mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-04 08:28:36 +00:00
fix(slack): keep fenced markdown tables intact in _to_mrkdwn
This commit is contained in:
parent
8195181783
commit
5851bd432a
@ -701,7 +701,16 @@ class SlackChannel(BaseChannel):
|
||||
"""Convert Markdown to Slack mrkdwn, including tables."""
|
||||
if not text:
|
||||
return ""
|
||||
code_blocks: list[str] = []
|
||||
|
||||
def _save_fence(m: re.Match) -> str:
|
||||
code_blocks.append(m.group(0))
|
||||
return f"\x00CB{len(code_blocks) - 1}\x00"
|
||||
|
||||
text = cls._CODE_FENCE_RE.sub(_save_fence, text)
|
||||
text = cls._TABLE_RE.sub(cls._convert_table, text)
|
||||
for i, block in enumerate(code_blocks):
|
||||
text = text.replace(f"\x00CB{i}\x00", block)
|
||||
return cls._fixup_mrkdwn(slackify_markdown(text)).rstrip("\n")
|
||||
|
||||
@classmethod
|
||||
|
||||
@ -714,3 +714,19 @@ def test_group_require_mention_accepts_camel_case_alias() -> None:
|
||||
)
|
||||
assert config.group_require_mention is True
|
||||
assert config.group_allow_from == ["C_OK"]
|
||||
|
||||
|
||||
def test_to_mrkdwn_keeps_fenced_markdown_tables_intact() -> None:
|
||||
text = "Intro\n\n```\n| a | b |\n| - | - |\n| 1 | 2 |\n```\n\nOutro"
|
||||
out = SlackChannel._to_mrkdwn(text)
|
||||
|
||||
assert "```\n| a | b |\n| - | - |\n| 1 | 2 |\n```" in out
|
||||
assert "**a**: 1" not in out
|
||||
assert "*a*: 1" not in out
|
||||
|
||||
|
||||
def test_to_mrkdwn_still_converts_unfenced_markdown_tables() -> None:
|
||||
out = SlackChannel._to_mrkdwn("| a | b |\n| - | - |\n| 1 | 2 |")
|
||||
|
||||
assert "| a | b |" not in out
|
||||
assert "a" in out and "1" in out and "b" in out and "2" in out
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user