fix(dingtalk): split table cells after escaped backslashes (#8429)

Co-authored-by: zjy-boom <174030947+zjy-boom@users.noreply.github.com>
This commit is contained in:
zjy-boom 2026-11-22 08:33:49 +08:00 committed by GitHub
parent 3de205c5ce
commit e09cd88c2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 14 deletions

View File

@ -119,20 +119,16 @@ _TABLE_SEPARATOR_RE = re.compile(r"^\|[-:| ]+\|$", re.MULTILINE)
def _split_markdown_table_row(row: str) -> list[str]:
r"""Keep escaped pipes and empty cells when removing the outer borders.
Known boundary: the single-character lookbehind in the split pattern
cannot distinguish an escaped pipe (``\|``) from an escaped backslash
immediately followed by a real separator (``\\|``). A cell ending in a
literal backslash therefore absorbs the next cell: ``| foo\\ | bar |``
splits as one cell instead of two. GFM counts consecutive backslashes
for this reason; doing so is out of scope here — this helper is
intentionally not a general Markdown parser.
"""
# ``(?<!\\)`` inspects only one preceding character, so the second
# backslash of ``\\|`` is mistaken for an escaping backslash and the
# pipe is kept whole. See the boundary note in the docstring above.
cells = re.split(r"(?<!\\)\|", row.strip())
"""Split at unescaped pipes, preserving the original cell text."""
row = row.strip()
cells = []
start = 0
# Consume backslash escapes in pairs so only an unescaped pipe ends a cell.
for match in re.finditer(r"\\.|[|]", row):
if match[0] == "|":
cells.append(row[start : match.start()])
start = match.end()
cells.append(row[start:])
if cells and cells[0] == "":
cells.pop(0)
if cells and cells[-1] == "":

View File

@ -1281,6 +1281,13 @@ class TestConvertMarkdownTable:
text = f"{header}\n|---|---|\n{row}"
assert _convert_markdown_table(text) == expected
@pytest.mark.parametrize("count", [1, 2, 3, 4])
def test_backslashes_before_pipe(self, count):
prefix = "path" + "\\" * count
text = f"| A | B |\n|---|---|\n|{prefix}|suffix|"
expected = f"> **A**: {prefix}|suffix\n" if count % 2 else f"> **A**: {prefix}\n> **B**: suffix\n"
assert _convert_markdown_table(text) == expected
def test_fallback_adapter_preserves_table_values(self):
text = "| Rule | Meaning |\n|---|---|\n| `foo\\|bar` | alternative |"
assert _adapt_markdown_for_dingtalk(text) == "> **Rule**: **foo\\|bar**\n> **Meaning**: alternative\n"