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
6 changes: 3 additions & 3 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down
34 changes: 34 additions & 0 deletions tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """<table>
<tr>
<th>Col1</th>
<th>Col2<table><tr><td>sub1</td><td>sub2</td></tr></table></th>
</tr>
<tr><td>a</td><td>b</td></tr>
</table>"""
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 = """<table>
<thead>
<tr>
<td>H1</td>
<td>H2<table><tr><td>x</td></tr></table></td>
</tr>
</thead>
<tbody>
<tr><td>a</td><td>b</td></tr>
</tbody>
</table>"""
out = md(html)
lines = out.strip().splitlines()
assert lines[0].startswith('| H1 | H2')
assert lines[1] == '| --- | --- |'
assert lines[2] == '| a | b |'