fix(dingtalk): validate Markdown table delimiter cells (#8138)

* fix(dingtalk): validate Markdown table delimiter cells

* fix(dingtalk): preserve tables with mismatched delimiter counts

---------

Co-authored-by: zjy-boom <174030947+zjy-boom@users.noreply.github.com>
Co-authored-by: Nan Gao <thunderggnn@gmail.com>
This commit is contained in:
zjy-boom 2026-11-23 11:50:12 +08:00 committed by GitHub
parent 17b8921ee3
commit ae53197923
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 1 deletions

View File

@ -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<delimiter>`+)(?!`)(?P<code>[^\n]+?)(?<!`)(?P=delimiter)(?!`)")
_HORIZONTAL_RULE_RE = re.compile(r"^-{3,}$", re.MULTILINE)
_TABLE_SEPARATOR_RE = re.compile(r"^\|[-:| ]+\|$", re.MULTILINE)
_TABLE_SEPARATOR_RE = re.compile(r"^\|[ \t]*:?-+:?[ \t]*(?:\|[ \t]*:?-+:?[ \t]*)*\|$", re.MULTILINE)
def _split_markdown_table_row(row: str) -> 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])

View File

@ -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"),
[

View File

@ -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.