Skip to content

Commit

Permalink
Merge pull request #104 from chrispy-snps/fix/97-101-102
Browse files Browse the repository at this point in the history
improve text normalization/escaping for preformatted/code contexts
  • Loading branch information
chrispy-snps authored Jan 15, 2024
2 parents e6e23fd + 2b22d23 commit c7718b6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
9 changes: 4 additions & 5 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,12 @@ def is_nested_node(el):
def process_text(self, el):
text = six.text_type(el) or ''

# dont remove any whitespace when handling pre or code in pre
if not (el.parent.name == 'pre'
or (el.parent.name == 'code'
and el.parent.parent.name == 'pre')):
# normalize whitespace if we're not inside a preformatted element
if not el.find_parent('pre'):
text = whitespace_re.sub(' ', text)

if el.parent.name != 'code' and el.parent.name != 'pre':
# escape special characters if we're not inside a preformatted or code element
if not el.find_parent(['pre', 'code', 'kbd', 'samp']):
text = self.escape(text)

# remove trailing whitespaces if any of the following condition is true:
Expand Down
12 changes: 10 additions & 2 deletions tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ def test_br():

def test_code():
inline_tests('code', '`')
assert md('<code>this_should_not_escape</code>') == '`this_should_not_escape`'
assert md('<code>*this_should_not_escape*</code>') == '`*this_should_not_escape*`'
assert md('<kbd>*this_should_not_escape*</kbd>') == '`*this_should_not_escape*`'
assert md('<samp>*this_should_not_escape*</samp>') == '`*this_should_not_escape*`'
assert md('<code><span>*this_should_not_escape*</span></code>') == '`*this_should_not_escape*`'
assert md('<code>this should\t\tnormalize</code>') == '`this should normalize`'
assert md('<code><span>this should\t\tnormalize</span></code>') == '`this should normalize`'


def test_del():
Expand Down Expand Up @@ -187,7 +192,10 @@ def test_p():
def test_pre():
assert md('<pre>test\n foo\nbar</pre>') == '\n```\ntest\n foo\nbar\n```\n'
assert md('<pre><code>test\n foo\nbar</code></pre>') == '\n```\ntest\n foo\nbar\n```\n'
assert md('<pre>this_should_not_escape</pre>') == '\n```\nthis_should_not_escape\n```\n'
assert md('<pre>*this_should_not_escape*</pre>') == '\n```\n*this_should_not_escape*\n```\n'
assert md('<pre><span>*this_should_not_escape*</span></pre>') == '\n```\n*this_should_not_escape*\n```\n'
assert md('<pre>\t\tthis should\t\tnot normalize</pre>') == '\n```\n\t\tthis should\t\tnot normalize\n```\n'
assert md('<pre><span>\t\tthis should\t\tnot normalize</span></pre>') == '\n```\n\t\tthis should\t\tnot normalize\n```\n'


def test_s():
Expand Down

0 comments on commit c7718b6

Please sign in to comment.