diff --git a/markdownify/__init__.py b/markdownify/__init__.py index d6d36a0..8461c1c 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -370,8 +370,13 @@ class MarkdownConverter(object): if is_headrow and not el.previous_sibling: # first row and is headline: print headline underline underline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n' - elif not el.previous_sibling and not el.parent.name != 'table': - # first row, not headline, and the parent is sth. like tbody: + elif (not el.previous_sibling + and (el.parent.name == 'table' + or (el.parent.name == 'tbody' + and not el.parent.previous_sibling))): + # first row, not headline, and: + # - the parent is table or + # - the parent is tbody at the beginning of a table. # print empty headline above this row overline += '| ' + ' | '.join([''] * len(cells)) + ' |' + '\n' overline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n' diff --git a/tests/test_tables.py b/tests/test_tables.py index 27d4403..e0c07ea 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -139,6 +139,26 @@ table_missing_head = """
""" +table_body = """ + + + + + + + + + + + + + + + + + +
FirstnameLastnameAge
JillSmith50
EveJackson94
""" + def test_table(): 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(): assert md(table_head_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' assert md(table_missing_text) == '\n\n| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |\n\n' assert md(table_missing_head) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' + assert md(table_body) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'