diff --git a/markdownify/__init__.py b/markdownify/__init__.py
index e15ecd4..bf765ec 100644
--- a/markdownify/__init__.py
+++ b/markdownify/__init__.py
@@ -152,13 +152,12 @@ class MarkdownConverter(object):
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:
diff --git a/tests/test_conversions.py b/tests/test_conversions.py
index da78649..1ecaf3c 100644
--- a/tests/test_conversions.py
+++ b/tests/test_conversions.py
@@ -70,7 +70,12 @@ def test_br():
def test_code():
inline_tests('code', '`')
- assert md('this_should_not_escape') == '`this_should_not_escape`'
+ assert md('*this_should_not_escape*') == '`*this_should_not_escape*`'
+ assert md('*this_should_not_escape*') == '`*this_should_not_escape*`'
+ assert md('*this_should_not_escape*') == '`*this_should_not_escape*`'
+ assert md('*this_should_not_escape*') == '`*this_should_not_escape*`'
+ assert md('this should\t\tnormalize') == '`this should normalize`'
+ assert md('this should\t\tnormalize') == '`this should normalize`'
def test_del():
@@ -187,7 +192,10 @@ def test_p():
def test_pre():
assert md('
test\n foo\nbar') == '\n```\ntest\n foo\nbar\n```\n' assert md('
test\n foo\nbar') == '\n```\ntest\n foo\nbar\n```\n'
- assert md('this_should_not_escape') == '\n```\nthis_should_not_escape\n```\n' + assert md('
*this_should_not_escape*') == '\n```\n*this_should_not_escape*\n```\n' + assert md('
*this_should_not_escape*') == '\n```\n*this_should_not_escape*\n```\n'
+ assert md('\t\tthis should\t\tnot normalize') == '\n```\n\t\tthis should\t\tnot normalize\n```\n' + assert md('
\t\tthis should\t\tnot normalize') == '\n```\n\t\tthis should\t\tnot normalize\n```\n'
def test_s():