fix(slack): keep fenced markdown tables intact in _to_mrkdwn
This commit is contained in:
@@ -701,7 +701,16 @@ class SlackChannel(BaseChannel):
|
|||||||
"""Convert Markdown to Slack mrkdwn, including tables."""
|
"""Convert Markdown to Slack mrkdwn, including tables."""
|
||||||
if not text:
|
if not text:
|
||||||
return ""
|
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)
|
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")
|
return cls._fixup_mrkdwn(slackify_markdown(text)).rstrip("\n")
|
||||||
|
|
||||||
@classmethod
|
@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_require_mention is True
|
||||||
assert config.group_allow_from == ["C_OK"]
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user