Skip to content

Commit c7329ac

Browse files
authored
Escape right square brackets (#187)
1 parent 3311f4d commit c7329ac

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

Diff for: markdownify/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def process_text(self, el, parent_tags=None):
305305

306306
# escape special characters if we're not inside a preformatted or code element
307307
if '_noformat' not in parent_tags:
308-
text = self.escape(text)
308+
text = self.escape(text, parent_tags)
309309

310310
# remove leading whitespace at the start or just after a
311311
# block-level element; remove traliing whitespace at the end
@@ -347,11 +347,11 @@ def should_convert_tag(self, tag):
347347
else:
348348
return True
349349

350-
def escape(self, text):
350+
def escape(self, text, parent_tags):
351351
if not text:
352352
return ''
353353
if self.options['escape_misc']:
354-
text = re.sub(r'([\\&<`[>~=+|])', r'\\\1', text)
354+
text = re.sub(r'([]\\&<`[>~=+|])', r'\\\1', text)
355355
# A sequence of one or more consecutive '-', preceded and
356356
# followed by whitespace or start/end of fragment, might
357357
# be confused with an underline of a header, or with a

Diff for: tests/test_escaping.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ def test_misc():
5151
assert md('-y', escape_misc=True) == '-y'
5252
assert md('+ x\n+ y\n', escape_misc=True) == '\\+ x\n\\+ y\n'
5353
assert md('`x`', escape_misc=True) == r'\`x\`'
54-
assert md('[text](link)', escape_misc=True) == r'\[text](link)'
54+
assert md('[text](notalink)', escape_misc=True) == r'\[text\](notalink)'
55+
assert md('<a href="link">text]</a>', escape_misc=True) == r'[text\]](link)'
56+
assert md('<a href="link">[text]</a>', escape_misc=True) == r'[\[text\]](link)'
5557
assert md('1. x', escape_misc=True) == r'1\. x'
5658
# assert md('1<span>.</span> x', escape_misc=True) == r'1\. x'
5759
assert md('<span>1.</span> x', escape_misc=True) == r'1\. x'

0 commit comments

Comments
 (0)