Skip to content

Commit e4df412

Browse files
authored
Support conversion of header rows in tables without th tag (#83)
* Fixed support for header row conversion for tables without th tag
1 parent 804a3f8 commit e4df412

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

markdownify/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,11 @@ def convert_th(self, el, text, convert_as_inline):
377377

378378
def convert_tr(self, el, text, convert_as_inline):
379379
cells = el.find_all(['td', 'th'])
380-
is_headrow = all([cell.name == 'th' for cell in cells])
380+
is_headrow = (
381+
all([cell.name == 'th' for cell in cells])
382+
or (not el.previous_sibling and not el.parent.name == 'tbody')
383+
or (not el.previous_sibling and el.parent.name == 'tbody' and len(el.parent.parent.find_all(['thead'])) < 1)
384+
)
381385
overline = ''
382386
underline = ''
383387
if is_headrow and not el.previous_sibling:

tests/test_tables.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@
119119
</tbody>
120120
</table>"""
121121

122+
table_head_body_missing_head = """<table>
123+
<thead>
124+
<tr>
125+
<td>Firstname</td>
126+
<td>Lastname</td>
127+
<td>Age</td>
128+
</tr>
129+
</thead>
130+
<tbody>
131+
<tr>
132+
<td>Jill</td>
133+
<td>Smith</td>
134+
<td>50</td>
135+
</tr>
136+
<tr>
137+
<td>Eve</td>
138+
<td>Jackson</td>
139+
<td>94</td>
140+
</tr>
141+
</tbody>
142+
</table>"""
143+
122144
table_missing_text = """<table>
123145
<thead>
124146
<tr>
@@ -187,6 +209,7 @@ def test_table():
187209
assert md(table_with_linebreaks) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith Jackson | 50 |\n| Eve | Jackson Smith | 94 |\n\n'
188210
assert md(table_with_header_column) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
189211
assert md(table_head_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
212+
assert md(table_head_body_missing_head) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
190213
assert md(table_missing_text) == '\n\n| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |\n\n'
191-
assert md(table_missing_head) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
192-
assert md(table_body) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
214+
assert md(table_missing_head) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
215+
assert md(table_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'

0 commit comments

Comments
 (0)