From eb74710597383b0bc33d51a95081ac136722dfe0 Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:31:38 -0600 Subject: [PATCH] Escape pipes in table cell content A literal '|' in a or was written to the Markdown table as-is, so it ended the cell early: 'a|b' produced '| a|b |', which re-parses as two cells and shifts the rest of the row. escape_misc=True happened to escape the pipe, but the default output and pipes inside (which skips escaping for preformatted content) were still broken. Escape unescaped pipes when emitting a cell. Already-escaped pipes are left alone so escape_misc output does not gain a second backslash. --- markdownify/__init__.py | 10 ++++++++-- tests/test_tables.py | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index d946d2c..3072050 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -45,6 +45,10 @@ # Find consecutive backtick sequences in a string re_backtick_runs = re.compile(r'`+') +# Find pipes that are not already escaped, so they can be escaped inside +# table cells without touching escapes that escape_misc already added +re_escape_table_pipe = re.compile(r'(? + Expression + a|b + a|b +""" + + +def test_table_pipes_are_escaped(): + assert md(table_with_pipes) == ( + '\n\n| Expression |\n| --- |\n' + '| a\\|b |\n' + '| `a\\|b` |\n\n' + ) + + +def test_table_pipes_not_double_escaped(): + assert md(table_with_pipes, escape_misc=True) == ( + '\n\n| Expression |\n| --- |\n' + '| a\\|b |\n' + '| `a\\|b` |\n\n' + )