diff --git a/markdownify/__init__.py b/markdownify/__init__.py index d946d2c..c6f378b 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -746,17 +746,17 @@ def convert_th(self, el, text, parent_tags): return ' ' + text.strip().replace("\n", " ") + ' |' * colspan def convert_tr(self, el, text, parent_tags): - cells = el.find_all(['td', 'th']) + cells = el.find_all(['td', 'th'], recursive=False) is_first_row = el.find_previous_sibling() is None is_headrow = ( all([cell.name == 'th' for cell in cells]) or (el.parent.name == 'thead' # avoid multiple tr in thead - and len(el.parent.find_all('tr')) == 1) + and len(el.parent.find_all('tr', recursive=False)) == 1) ) is_head_row_missing = ( (is_first_row and not el.parent.name == 'tbody') - or (is_first_row and el.parent.name == 'tbody' and len(el.parent.parent.find_all(['thead'])) < 1) + or (is_first_row and el.parent.name == 'tbody' and len(el.parent.parent.find_all(['thead'], recursive=False)) < 1) ) overline = '' underline = '' diff --git a/tests/test_tables.py b/tests/test_tables.py index 7e0670c..de8822d 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -319,3 +319,37 @@ 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_nested_in_first_row(): + html = """ + + + + + +
Col1Col2
sub1sub2
ab
""" + out = md(html) + lines = out.strip().splitlines() + assert lines[0].startswith('| Col1 | Col2') + assert lines[1] == '| --- | --- |' + assert lines[2] == '| a | b |' + + +def test_table_nested_in_thead_and_tbody(): + html = """ + + + + + + + + + +
H1H2
x
ab
""" + out = md(html) + lines = out.strip().splitlines() + assert lines[0].startswith('| H1 | H2') + assert lines[1] == '| --- | --- |' + assert lines[2] == '| a | b |'