Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ def test_hn_atx_headings():
assert md('<h2>Hello</h2>', heading_style=ATX) == '\n\n## Hello\n\n'


def test_hn_literal_trailing_hashes():
assert md('<h1>Issue #</h1>', heading_style=ATX) == '\n\n# Issue \\#\n\n'
assert md('<h3>###</h3>') == '\n\n### \\###\n\n'
assert md('<h3>C#</h3>') == '\n\n### C#\n\n'
assert md('<h2>Issue #</h2>', heading_style=ATX_CLOSED) == '\n\n## Issue # ##\n\n'


def test_hn_atx_closed_headings():
assert md('<h1>Hello</h1>', heading_style=ATX_CLOSED) == '\n\n# Hello #\n\n'
assert md('<h2>Hello</h2>', heading_style=ATX_CLOSED) == '\n\n## Hello ##\n\n'
Expand Down