From 41d59c3b89494c16e2dfa83bb9b5cf831eed2f5b Mon Sep 17 00:00:00 2001 From: Xubin Ren Date: Tue, 17 Mar 2026 08:40:39 +0000 Subject: [PATCH] test(feishu): cover heading and table markdown rendering --- nanobot/channels/feishu.py | 13 +++--- tests/test_feishu_markdown_rendering.py | 57 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 tests/test_feishu_markdown_rendering.py diff --git a/nanobot/channels/feishu.py b/nanobot/channels/feishu.py index bbe5281..d450e25 100644 --- a/nanobot/channels/feishu.py +++ b/nanobot/channels/feishu.py @@ -437,8 +437,10 @@ class FeishuChannel(BaseChannel): _CODE_BLOCK_RE = re.compile(r"(```[\s\S]*?```)", re.MULTILINE) - # Markdown bold/italic patterns that need to be stripped for table cells + # Markdown formatting patterns that should be stripped from plain-text + # surfaces like table cells and heading text. _MD_BOLD_RE = re.compile(r"\*\*(.+?)\*\*") + _MD_BOLD_UNDERSCORE_RE = re.compile(r"__(.+?)__") _MD_ITALIC_RE = re.compile(r"(? None: + table = FeishuChannel._parse_md_table( + """ +| **Name** | __Status__ | *Notes* | ~~State~~ | +| --- | --- | --- | --- | +| **Alice** | __Ready__ | *Fast* | ~~Old~~ | +""" + ) + + assert table is not None + assert [col["display_name"] for col in table["columns"]] == [ + "Name", + "Status", + "Notes", + "State", + ] + assert table["rows"] == [ + {"c0": "Alice", "c1": "Ready", "c2": "Fast", "c3": "Old"} + ] + + +def test_split_headings_strips_embedded_markdown_before_bolding() -> None: + channel = FeishuChannel.__new__(FeishuChannel) + + elements = channel._split_headings("# **Important** *status* ~~update~~") + + assert elements == [ + { + "tag": "div", + "text": { + "tag": "lark_md", + "content": "**Important status update**", + }, + } + ] + + +def test_split_headings_keeps_markdown_body_and_code_blocks_intact() -> None: + channel = FeishuChannel.__new__(FeishuChannel) + + elements = channel._split_headings( + "# **Heading**\n\nBody with **bold** text.\n\n```python\nprint('hi')\n```" + ) + + assert elements[0] == { + "tag": "div", + "text": { + "tag": "lark_md", + "content": "**Heading**", + }, + } + assert elements[1]["tag"] == "markdown" + assert "Body with **bold** text." in elements[1]["content"] + assert "```python\nprint('hi')\n```" in elements[1]["content"]