From f11d29625dfcb24bbafec88724f5bba01b13390f Mon Sep 17 00:00:00 2001 From: Hugohong258 <282228220+Hugohong258@users.noreply.github.com> Date: Sat, 28 Nov 2026 09:03:53 +0800 Subject: [PATCH] Emit one table header across HTML row groups --- markdownify/__init__.py | 9 +++++++++ tests/test_tables.py | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index d946d2c..8be7668 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -748,6 +748,15 @@ def convert_th(self, el, text, parent_tags): def convert_tr(self, el, text, parent_tags): cells = el.find_all(['td', 'th']) is_first_row = el.find_previous_sibling() is None + if is_first_row and el.parent.name in ('tbody', 'tfoot'): + # A new row group continues the same Markdown table. + is_first_row = not any( + sibling.name == 'tr' or ( + sibling.name in ('thead', 'tbody', 'tfoot') + and sibling.find('tr', recursive=False) is not None + ) + for sibling in el.parent.previous_siblings + ) is_headrow = ( all([cell.name == 'th' for cell in cells]) or (el.parent.name == 'thead' diff --git a/tests/test_tables.py b/tests/test_tables.py index 7e0670c..7b19977 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -319,3 +319,24 @@ 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_multiple_table_bodies_share_a_header(): + html = ('' + '' + '
A
B
') + assert md(html) == '\n\n| |\n| --- |\n| A |\n| B |\n\n' + assert md(html, table_infer_header=True) == '\n\n| A |\n| --- |\n| B |\n\n' + + +def test_header_cells_in_later_body_do_not_repeat_separator(): + html = ('' + '' + '
Heading
A
B
') + assert md(html) == '\n\n| Heading |\n| --- |\n| A |\n| B |\n\n' + + +def test_footer_after_direct_rows_does_not_repeat_header(): + html = ('' + '
Heading
Total
') + assert md(html) == '\n\n| Heading |\n| --- |\n| Total |\n\n'