test(feishu): cover heading and table markdown rendering

This commit is contained in:
Xubin Ren
2026-03-17 08:40:39 +00:00
committed by Xubin Ren
parent 9afbf386c4
commit 41d59c3b89
2 changed files with 63 additions and 7 deletions

View File

@@ -437,8 +437,10 @@ class FeishuChannel(BaseChannel):
_CODE_BLOCK_RE = re.compile(r"(```[\s\S]*?```)", re.MULTILINE) _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_RE = re.compile(r"\*\*(.+?)\*\*")
_MD_BOLD_UNDERSCORE_RE = re.compile(r"__(.+?)__")
_MD_ITALIC_RE = re.compile(r"(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)") _MD_ITALIC_RE = re.compile(r"(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)")
_MD_STRIKE_RE = re.compile(r"~~(.+?)~~") _MD_STRIKE_RE = re.compile(r"~~(.+?)~~")
@@ -451,6 +453,7 @@ class FeishuChannel(BaseChannel):
""" """
# Remove bold markers # Remove bold markers
text = cls._MD_BOLD_RE.sub(r"\1", text) text = cls._MD_BOLD_RE.sub(r"\1", text)
text = cls._MD_BOLD_UNDERSCORE_RE.sub(r"\1", text)
# Remove italic markers # Remove italic markers
text = cls._MD_ITALIC_RE.sub(r"\1", text) text = cls._MD_ITALIC_RE.sub(r"\1", text)
# Remove strikethrough markers # Remove strikethrough markers
@@ -532,12 +535,8 @@ class FeishuChannel(BaseChannel):
before = protected[last_end:m.start()].strip() before = protected[last_end:m.start()].strip()
if before: if before:
elements.append({"tag": "markdown", "content": before}) elements.append({"tag": "markdown", "content": before})
text = m.group(2).strip() text = self._strip_md_formatting(m.group(2).strip())
# Avoid double bold markers if text already contains them display_text = f"**{text}**" if text else ""
if text.startswith("**") and text.endswith("**"):
display_text = text
else:
display_text = f"**{text}**"
elements.append({ elements.append({
"tag": "div", "tag": "div",
"text": { "text": {

View File

@@ -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"]