Skip to content

Your lightweight open-source personal agent.

test(feishu): cover heading and table markdown rendering

Xubin Ren41d59c3b8949
diff --git a/nanobot/channels/feishu.py b/nanobot/channels/feishu.py
index bbe5281b5..d450e25f5 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"(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)")
_MD_STRIKE_RE = re.compile(r"~~(.+?)~~")
@@ -451,6 +453,7 @@ class FeishuChannel(BaseChannel):
"""
# Remove bold markers
text = cls._MD_BOLD_RE.sub(r"\1", text)
+ text = cls._MD_BOLD_UNDERSCORE_RE.sub(r"\1", text)
# Remove italic markers
text = cls._MD_ITALIC_RE.sub(r"\1", text)
# Remove strikethrough markers
@@ -532,12 +535,8 @@ class FeishuChannel(BaseChannel):
before = protected[last_end:m.start()].strip()
if before:
elements.append({"tag": "markdown", "content": before})
- text = m.group(2).strip()
- # Avoid double bold markers if text already contains them
- if text.startswith("**") and text.endswith("**"):
- display_text = text
- else:
- display_text = f"**{text}**"
+ text = self._strip_md_formatting(m.group(2).strip())
+ display_text = f"**{text}**" if text else ""
elements.append({
"tag": "div",
"text": {
diff --git a/tests/test_feishu_markdown_rendering.py b/tests/test_feishu_markdown_rendering.py
new file mode 100644
index 000000000..6812a21aa
--- /dev/null
+++ b/tests/test_feishu_markdown_rendering.py
@@ -0,0 +1,57 @@
+from nanobot.channels.feishu import FeishuChannel
+
+
+def test_parse_md_table_strips_markdown_formatting_in_headers_and_cells() -> 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"]