From 86b878d37b36c68104a6b37a0513553345c357e7 Mon Sep 17 00:00:00 2001
From: Boulea7 <145515129+Boulea7@users.noreply.github.com>
Date: Thu, 3 Dec 2026 13:23:49 +0800
Subject: [PATCH 1/2] Allow block boundaries to render as breaks in table cells
---
README.rst | 6 ++++++
markdownify/__init__.py | 24 ++++++++++++++++++++++++
markdownify/main.py | 2 ++
tests/test_tables.py | 22 ++++++++++++++++++++++
4 files changed, 54 insertions(+)
diff --git a/README.rst b/README.rst
index 059a68f..99d5ca3 100644
--- a/README.rst
+++ b/README.rst
@@ -139,6 +139,12 @@ keep_inline_images_in
that should be allowed to contain inline images, for example ``['td']``.
Defaults to an empty list.
+table_cell_block_breaks
+ Set to ``True`` to separate paragraphs, divs and list items inside a table
+ cell with ``
``. For example, ``
One Two | ``
+ produces a cell containing ``One
Two``. The Markdown renderer must
+ support inline HTML. Defaults to ``False``.
+
table_infer_header
Controls handling of tables with no header row (as indicated by ````
or ````). When set to ``True``, the first body row is used as the header row.
diff --git a/markdownify/__init__.py b/markdownify/__init__.py
index d946d2c..50a67d1 100644
--- a/markdownify/__init__.py
+++ b/markdownify/__init__.py
@@ -195,6 +195,7 @@ class DefaultOptions:
strong_em_symbol = ASTERISK
sub_symbol = ''
sup_symbol = ''
+ table_cell_block_breaks = False
table_infer_header = False
wrap = False
wrap_width = 80
@@ -279,6 +280,9 @@ def _can_ignore(el):
or node.name in {'td', 'th'} # table cells
):
parent_tags_for_children.add('_inline')
+ if (self.options['table_cell_block_breaks'] and node.name in {'td', 'th'}
+ and self.should_convert_tag(node.name)):
+ parent_tags_for_children.add('_table_cell_blocks')
# if this tag is a preformatted element, add a '_noformat' parent pseudo-tag
if node.name in {'pre', 'code', 'kbd', 'samp'}:
@@ -289,6 +293,8 @@ def _can_ignore(el):
self.process_element(el, parent_tags=parent_tags_for_children)
for el in children_to_convert
]
+ if '_table_cell_blocks' in parent_tags_for_children and '_noformat' not in parent_tags_for_children:
+ child_strings = self._separate_table_cell_blocks(children_to_convert, child_strings)
# Remove empty string values.
child_strings = [s for s in child_strings if s]
@@ -733,6 +739,24 @@ def convert_caption(self, el, text, parent_tags):
def convert_figcaption(self, el, text, parent_tags):
return '\n\n' + text.strip() + '\n\n'
+ def _separate_table_cell_blocks(self, children, child_strings):
+ lines = []
+ inline = []
+ for child, text in zip(children, child_strings):
+ if not text:
+ continue
+ if child.name in ('div', 'p', 'ul', 'ol', 'li') and self.should_convert_tag(child.name):
+ block = text.strip()
+ if block:
+ lines.extend([''.join(inline).strip(), block])
+ inline = []
+ else:
+ inline.append(text)
+ if not lines:
+ return child_strings
+ lines.append(''.join(inline).strip())
+ return [' '.join(line for line in lines if line)]
+
def convert_td(self, el, text, parent_tags):
colspan = 1
if 'colspan' in el.attrs and el['colspan'].isdigit():
diff --git a/markdownify/main.py b/markdownify/main.py
index ba70671..d1faa5c 100755
--- a/markdownify/main.py
+++ b/markdownify/main.py
@@ -63,6 +63,8 @@ def main(argv=sys.argv[1:]):
"should be converted to markdown images instead, this option can "
"be set to a list of parent tags that should be allowed to "
"contain inline images.")
+ parser.add_argument('--table-cell-block-breaks', action='store_true',
+ help='Separate paragraphs, divs and list items in table cells with .')
parser.add_argument('--table-infer-header', dest='table_infer_header',
action='store_true',
help="When a table has no header row (as indicated by '' "
diff --git a/tests/test_tables.py b/tests/test_tables.py
index 7e0670c..446c8ae 100644
--- a/tests/test_tables.py
+++ b/tests/test_tables.py
@@ -319,3 +319,25 @@ def test_table_infer_header():
assert md(table_with_colspan, table_infer_header=True) == '\n\n| Name | | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
assert md(table_with_undefined_colspan, table_infer_header=True) == '\n\n| Name | Age |\n| --- | --- |\n| Jill | Smith |\n\n'
assert md(table_with_colspan_missing_head, table_infer_header=True) == '\n\n| Name | | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
+
+
+def test_table_cell_block_breaks():
+ html = (
+ ''
+ )
+ assert md(html) == '\n\n| First Second |\n| --- |\n| * Item one * Item two |\n\n'
+ assert md(html, table_cell_block_breaks=True) == (
+ '\n\n| First Second |\n| --- |\n| * Item one * Item two |\n\n'
+ )
+
+
+def test_table_cell_block_breaks_keep_inline_content_and_ignore_empty_blocks():
+ html = (
+ '| Header |
|---|
| '
+ 'A\n wordEnd'
+ ' | '
+ )
+ assert md(html, table_cell_block_breaks=True) == (
+ '\n\n| Header |\n| --- |\n| A *word* **Bold** part End |\n\n'
+ )
From 14bf8cabfa105d3ab47186a6f9402056d68aaf0f Mon Sep 17 00:00:00 2001
From: Boulea7 <145515129+Boulea7@users.noreply.github.com>
Date: Thu, 3 Dec 2026 13:29:28 +0800
Subject: [PATCH 2/2] Preserve spacing around block content in transparent
wrappers
---
markdownify/__init__.py | 11 ++++++++++-
tests/test_tables.py | 14 ++++++++++++++
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/markdownify/__init__.py b/markdownify/__init__.py
index 50a67d1..b0b4f37 100644
--- a/markdownify/__init__.py
+++ b/markdownify/__init__.py
@@ -755,7 +755,16 @@ def _separate_table_cell_blocks(self, children, child_strings):
if not lines:
return child_strings
lines.append(''.join(inline).strip())
- return [' '.join(line for line in lines if line)]
+ lines = [line for line in lines if line]
+ if len(lines) < 2:
+ return child_strings
+
+ # A surrounding tag may pass this text through to adjacent words.
+ # Trim whitespace only at the boundaries replaced by a break.
+ original = ''.join(text for text in child_strings if text)
+ leading = original[:len(original) - len(original.lstrip())]
+ trailing = original[len(original.rstrip()):]
+ return [leading + ' '.join(lines) + trailing]
def convert_td(self, el, text, parent_tags):
colspan = 1
diff --git a/tests/test_tables.py b/tests/test_tables.py
index 446c8ae..1648553 100644
--- a/tests/test_tables.py
+++ b/tests/test_tables.py
@@ -341,3 +341,17 @@ def test_table_cell_block_breaks_keep_inline_content_and_ignore_empty_blocks():
assert md(html, table_cell_block_breaks=True) == (
'\n\n| Header |\n| --- |\n| A *word* **Bold** part End |\n\n'
)
+
+
+def test_table_cell_block_breaks_preserve_spaces_around_a_transparent_wrapper():
+ html = (
+ '| Header |
|---|
| '
+ 'IntroTail'
+ ' | '
+ )
+ expected = '\n\n| Header |\n| --- |\n| Intro Alpha Beta Tail |\n\n'
+ assert md(html, table_cell_block_breaks=True) == expected
+
+ html = html.replace('aside', 'div')
+ assert md(html, table_cell_block_breaks=True, strip=['div']) == expected
+ assert md(html, table_cell_block_breaks=True, convert=['table', 'tr', 'th', 'td', 'p']) == expected
|