diff --git a/backend/app/channels/dingtalk.py b/backend/app/channels/dingtalk.py index 6918cc37cd..abfc54c89c 100644 --- a/backend/app/channels/dingtalk.py +++ b/backend/app/channels/dingtalk.py @@ -116,7 +116,7 @@ _FENCED_CODE_BLOCK_RE = re.compile(r"```(\w*)\n(.*?)```", re.DOTALL) # Match complete backtick runs of equal length so shorter runs stay inside the code. _INLINE_CODE_RE = re.compile(r"(?`+)(?!`)(?P[^\n]+?)(? list[str]: @@ -147,6 +147,10 @@ def _convert_markdown_table(text: str) -> str: # Detect table: header row followed by separator row if i + 1 < len(lines) and line.strip().startswith("|") and _TABLE_SEPARATOR_RE.match(lines[i + 1].strip()): headers = _split_markdown_table_row(line) + if len(headers) != len(_split_markdown_table_row(lines[i + 1])): + result.append(line) + i += 1 + continue i += 2 # skip header + separator while i < len(lines) and lines[i].strip().startswith("|"): cells = _split_markdown_table_row(lines[i]) diff --git a/backend/tests/test_dingtalk_channel.py b/backend/tests/test_dingtalk_channel.py index 78980139a3..0f22cb98fb 100644 --- a/backend/tests/test_dingtalk_channel.py +++ b/backend/tests/test_dingtalk_channel.py @@ -1278,6 +1278,24 @@ class TestAdaptMarkdownForDingtalk: class TestConvertMarkdownTable: + @pytest.mark.parametrize("separator", ["|---|---|", "|---|---|---|---|", "|---||---|"]) + def test_invalid_three_column_table_preserves_text(self, separator): + text = f"| A | B | C |\n{separator}\n| 1 | 2 | 3 |" + assert _adapt_markdown_for_dingtalk(text) == text + + def test_table_in_fenced_code_remains_literal(self): + text = "```text\n| A | B |\n|---|---|\n| 1 | 2 |\n```" + assert _adapt_markdown_for_dingtalk(text) == "> **text**\n> | A | B |\n> |---|---|\n> | 1 | 2 |\n" + + def test_tab_padded_delimiter_cells(self): + text = "| Key | Value |\n|\t---\t|\t:---:\t|\n| a | b |" + assert _adapt_markdown_for_dingtalk(text) == "> **Key**: a\n> **Value**: b\n" + + @pytest.mark.parametrize("separator", ["| : | : |", "| --- | |", "| -- -- | --- |"], ids=["missing-hyphens", "empty-cell", "internal-space"]) + def test_invalid_delimiter_cells_preserve_text(self, separator): + text = f"| Key | Value |\n{separator}\n| a | b |" + assert _adapt_markdown_for_dingtalk(text) == text + @pytest.mark.parametrize( ("header", "row", "expected"), [ diff --git a/docs/guide/im-channels.md b/docs/guide/im-channels.md index 25e9f4932a..9c1f11b255 100644 --- a/docs/guide/im-channels.md +++ b/docs/guide/im-channels.md @@ -271,6 +271,8 @@ backtick delimiters to bold. For spans with non-space content, it removes one ASCII space from each end when both ends are padded. Tables display as labeled blockquotes, preserving escaped pipes (`\|`) within cells and empty leading or trailing cells so values stay under the correct column labels. +Each delimiter cell requires hyphens with optional alignment colons and accepts +space or tab padding. Malformed delimiter rows are left as text. DingTalk token caching uses a two-hour fallback when the provider's token lifetime is invalid or no longer than the five-minute refresh margin, avoiding a refresh on every request.