Skip to content

Commit ebb9ea7

Browse files
authored
Fix detection of "first row, not headline" (#63)
Improved handling of "first row, not headline". Works for tables with 1) neither thead nor tbody 2) tbody but no thead
1 parent 87b9f6c commit ebb9ea7

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

markdownify/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,13 @@ def convert_tr(self, el, text, convert_as_inline):
370370
if is_headrow and not el.previous_sibling:
371371
# first row and is headline: print headline underline
372372
underline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
373-
elif not el.previous_sibling and not el.parent.name != 'table':
374-
# first row, not headline, and the parent is sth. like tbody:
373+
elif (not el.previous_sibling
374+
and (el.parent.name == 'table'
375+
or (el.parent.name == 'tbody'
376+
and not el.parent.previous_sibling))):
377+
# first row, not headline, and:
378+
# - the parent is table or
379+
# - the parent is tbody at the beginning of a table.
375380
# print empty headline above this row
376381
overline += '| ' + ' | '.join([''] * len(cells)) + ' |' + '\n'
377382
overline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'

tests/test_tables.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,26 @@
139139
</tr>
140140
</table>"""
141141

142+
table_body = """<table>
143+
<tbody>
144+
<tr>
145+
<td>Firstname</td>
146+
<td>Lastname</td>
147+
<td>Age</td>
148+
</tr>
149+
<tr>
150+
<td>Jill</td>
151+
<td>Smith</td>
152+
<td>50</td>
153+
</tr>
154+
<tr>
155+
<td>Eve</td>
156+
<td>Jackson</td>
157+
<td>94</td>
158+
</tr>
159+
</tbody>
160+
</table>"""
161+
142162

143163
def test_table():
144164
assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
@@ -148,3 +168,4 @@ def test_table():
148168
assert md(table_head_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
149169
assert md(table_missing_text) == '\n\n| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |\n\n'
150170
assert md(table_missing_head) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
171+
assert md(table_body) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'

0 commit comments

Comments
 (0)