From e4df41225da327aeb1ca5acd87be02518f8f8865 Mon Sep 17 00:00:00 2001 From: huuya <32855384+huuyafwww@users.noreply.github.com> Date: Wed, 27 Mar 2024 05:32:36 +0900 Subject: [PATCH] Support conversion of header rows in tables without th tag (#83) * Fixed support for header row conversion for tables without th tag --- markdownify/__init__.py | 6 +++++- tests/test_tables.py | 27 +++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 1e7b787..b67c32b 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -377,7 +377,11 @@ def convert_th(self, el, text, convert_as_inline): def convert_tr(self, el, text, convert_as_inline): cells = el.find_all(['td', 'th']) - is_headrow = all([cell.name == 'th' for cell in cells]) + is_headrow = ( + all([cell.name == 'th' for cell in cells]) + or (not el.previous_sibling and not el.parent.name == 'tbody') + or (not el.previous_sibling and el.parent.name == 'tbody' and len(el.parent.parent.find_all(['thead'])) < 1) + ) overline = '' underline = '' if is_headrow and not el.previous_sibling: diff --git a/tests/test_tables.py b/tests/test_tables.py index 334bfb7..ebbb146 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -119,6 +119,28 @@ """ +table_head_body_missing_head = """ + + + + + + + + + + + + + + + + + + + +
FirstnameLastnameAge
JillSmith50
EveJackson94
""" + table_missing_text = """ @@ -187,6 +209,7 @@ def test_table(): assert md(table_with_linebreaks) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith Jackson | 50 |\n| Eve | Jackson Smith | 94 |\n\n' assert md(table_with_header_column) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' assert md(table_head_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' + assert md(table_head_body_missing_head) == '\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' + assert md(table_missing_head) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' + assert md(table_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'