From 98bd79fbf07e5199af6d194da426fdf4abc31169 Mon Sep 17 00:00:00 2001
From: Gary <182863204+gary1110086@users.noreply.github.com>
Date: Tue, 27 Oct 2026 22:50:08 +0800
Subject: [PATCH] Match complete heading tag names
Anchor the shared heading pattern so custom elements such as h2-panel do not receive heading formatting or inline-only child conversion. Cover inline text, block children, and image preservation.
---
markdownify/__init__.py | 2 +-
tests/test_custom_converter.py | 17 +++++++++++++++++
2 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/markdownify/__init__.py b/markdownify/__init__.py
index 28cdaf6..72329c8 100644
--- a/markdownify/__init__.py
+++ b/markdownify/__init__.py
@@ -10,7 +10,7 @@
re_whitespace = re.compile(r'[\t ]+')
re_all_whitespace = re.compile(r'[\t \r\n]+')
re_newline_whitespace = re.compile(r'[\t \r\n]*[\r\n][\t \r\n]*')
-re_html_heading = re.compile(r'h(\d+)')
+re_html_heading = re.compile(r'h(\d+)$')
re_pre_lstrip1 = re.compile(r'^ *\n')
re_pre_rstrip1 = re.compile(r'\n *$')
re_pre_lstrip = re.compile(r'^[ \n]*\n')
diff --git a/tests/test_custom_converter.py b/tests/test_custom_converter.py
index 00a83fc..682280d 100644
--- a/tests/test_custom_converter.py
+++ b/tests/test_custom_converter.py
@@ -1,5 +1,6 @@
from markdownify import MarkdownConverter
from bs4 import BeautifulSoup
+import pytest
class UnitTestConverter(MarkdownConverter):
@@ -42,3 +43,19 @@ def test_soup():
html = 'test'
soup = BeautifulSoup(html, 'html.parser')
assert MarkdownConverter().convert_soup(soup) == '**test**'
+
+
+@pytest.mark.parametrize('tag_name', ['h1-label', 'h2-status', 'h10-widget'])
+def test_heading_prefix_does_not_make_custom_tag_a_heading(tag_name):
+ html = 'Before <{0}>Status{0}> after'.format(tag_name)
+ assert MarkdownConverter().convert(html) == 'Before Status after'
+
+
+def test_heading_prefix_custom_tag_preserves_block_children():
+ html = 'First
Second
'
+ assert MarkdownConverter().convert(html) == 'First\n\nSecond'
+
+
+def test_heading_prefix_custom_tag_preserves_images():
+ html = '
'
+ assert MarkdownConverter().convert(html) == ''