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 = """
Firstname | +Lastname | +Age | +
Jill | +Smith | +50 | +
Eve | +Jackson | +94 | +