From 16c4a882919fdb4eedff741f5ec11d29880d8805 Mon Sep 17 00:00:00 2001 From: Old Ma <29540617+bug-origin@users.noreply.github.com> Date: Sat, 28 Nov 2026 19:20:56 +0900 Subject: [PATCH] Preserve literal trailing hashes in ATX headings --- markdownify/__init__.py | 2 ++ tests/test_conversions.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index d946d2c..4f89c13 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -573,6 +573,8 @@ def convert_hN(self, n, el, text, parent_tags): hashes = '#' * n if style == ATX_CLOSED: return '\n\n%s %s %s\n\n' % (hashes, text, hashes) + # A final run of hashes after a space (or alone) would close the heading. + text = re.sub(r'(^| )(#+)$', r'\1\\\2', text) return '\n\n%s %s\n\n' % (hashes, text) def convert_hr(self, el, text, parent_tags): diff --git a/tests/test_conversions.py b/tests/test_conversions.py index c95483c..ff7dbfe 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -223,6 +223,13 @@ def test_hn_atx_headings(): assert md('