Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,12 @@ def underline(self, text, pad_char):
return '\n\n%s\n%s\n\n' % (text, pad_char * len(text)) if text else ''

def convert_a(self, el, text, parent_tags):
if '_noformat' in parent_tags:
href = el.get('href')
if '_noformat' in parent_tags or not href:
return text
prefix, suffix, text = chomp(text)
if not text:
return ''
href = el.get('href')
title = el.get('title')
# For the replacement see #29: text nodes underscores are escaped
if (self.options['autolinks']
Expand All @@ -453,7 +453,7 @@ def convert_a(self, el, text, parent_tags):
if self.options['default_title'] and not title:
title = href
title_part = ' "%s"' % title.replace('"', r'\"') if title else ''
return '%s[%s](%s%s)%s' % (prefix, text, href, title_part, suffix) if href else text
return '%s[%s](%s%s)%s' % (prefix, text, href, title_part, suffix)

convert_b = abstract_inline_conversion(lambda self: 2 * self.options['strong_em_symbol'])

Expand Down
8 changes: 8 additions & 0 deletions tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def test_a_with_title():
assert md('<a href="https://google.com">https://google.com</a>', default_title=True) == '[https://google.com](https://google.com "https://google.com")'


def test_a_without_destination_preserves_whitespace():
assert md('left<a> middle </a>right') == 'left middle right'
assert md('left<a href=""> middle </a>right') == 'left middle right'
assert md('left<a name="section"> </a>right') == 'left right'
assert md('left<a><strong> middle </strong></a>right') == 'left **middle** right'
assert md('left<a></a>right') == 'leftright'


def test_a_shortcut():
text = md('<a href="http://google.com">http://google.com</a>')
assert text == '<http://google.com>'
Expand Down