Skip to content

Commit 596be57

Browse files
facelessuserwaylan
authored andcommitted
Allow hashes to be escaped in headers (#763)
Adjust pattern to allow for escaped hashes, but take care to not treat escaped escapes before hashes as escaped hashes. Close #762.
1 parent ab24c23 commit 596be57

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Diff for: docs/change_log/release-3.1.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ The following bug fixes are included in the 3.1 release:
3636
(#731).
3737
* Double escaping of block code has been eliminated (#725).
3838
* Problems with newlines in references has been fixed (#742).
39+
* Escaped `#` are now handled in header syntax (#762).

Diff for: markdown/blockprocessors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ class HashHeaderProcessor(BlockProcessor):
445445
""" Process Hash Headers. """
446446

447447
# Detect a header at start of any line in block
448-
RE = re.compile(r'(^|\n)(?P<level>#{1,6})(?P<header>.*?)#*(\n|$)')
448+
RE = re.compile(r'(?:^|\n)(?P<level>#{1,6})(?P<header>(?:\\.|[^\\])*?)#*(?:\n|$)')
449449

450450
def test(self, parent, block):
451451
return bool(self.RE.search(block))

Diff for: tests/test_syntax/blocks/test_headers.py

+20
Original file line numberDiff line numberDiff line change
@@ -708,3 +708,23 @@ def test_p_followed_by_hash(self):
708708
"""
709709
)
710710
)
711+
712+
def test_escaped_hash(self):
713+
self.assertMarkdownRenders(
714+
"### H3 \\###",
715+
self.dedent(
716+
"""
717+
<h3>H3 #</h3>
718+
"""
719+
)
720+
)
721+
722+
def test_unescaped_hash(self):
723+
self.assertMarkdownRenders(
724+
"### H3 \\\\###",
725+
self.dedent(
726+
"""
727+
<h3>H3 \\</h3>
728+
"""
729+
)
730+
)

0 commit comments

Comments
 (0)