Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
21 changes: 21 additions & 0 deletions tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ('<table><tbody></tbody>'
'<tbody><tr><td>A</td></tr></tbody>'
'<tbody><tr><td>B</td></tr></tbody></table>')
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 = ('<table><thead><tr><th>Heading</th></tr></thead>'
'<tbody><tr><th>A</th></tr></tbody>'
'<tbody><tr><th>B</th></tr></tbody></table>')
assert md(html) == '\n\n| Heading |\n| --- |\n| A |\n| B |\n\n'


def test_footer_after_direct_rows_does_not_repeat_header():
html = ('<table><tr><th>Heading</th></tr>'
'<tfoot><tr><td>Total</td></tr></tfoot></table>')
assert md(html) == '\n\n| Heading |\n| --- |\n| Total |\n\n'