Skip to content

Commit

Permalink
Allow hashes to be escaped in headers (Python-Markdown#763)
Browse files Browse the repository at this point in the history
Adjust pattern to allow for escaped hashes, but take care to not treat
escaped escapes before hashes as escaped hashes. Close Python-Markdown#762.
  • Loading branch information
facelessuser authored and waylan committed Dec 22, 2018
1 parent ab24c23 commit 596be57
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/change_log/release-3.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ The following bug fixes are included in the 3.1 release:
(#731).
* Double escaping of block code has been eliminated (#725).
* Problems with newlines in references has been fixed (#742).
* Escaped `#` are now handled in header syntax (#762).
2 changes: 1 addition & 1 deletion markdown/blockprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ class HashHeaderProcessor(BlockProcessor):
""" Process Hash Headers. """

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

def test(self, parent, block):
return bool(self.RE.search(block))
Expand Down
20 changes: 20 additions & 0 deletions tests/test_syntax/blocks/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,3 +708,23 @@ def test_p_followed_by_hash(self):
"""
)
)

def test_escaped_hash(self):
self.assertMarkdownRenders(
"### H3 \\###",
self.dedent(
"""
<h3>H3 #</h3>
"""
)
)

def test_unescaped_hash(self):
self.assertMarkdownRenders(
"### H3 \\\\###",
self.dedent(
"""
<h3>H3 \\</h3>
"""
)
)

0 comments on commit 596be57

Please sign in to comment.