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
4 changes: 4 additions & 0 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
# Find consecutive backtick sequences in a string
re_backtick_runs = re.compile(r'`+')

# CommonMark URI autolinks require a scheme and exclude spaces/angle brackets.
re_uri_autolink = re.compile(r'[A-Za-z][A-Za-z0-9+.-]{1,31}:[^<>\x00-\x20\x7f]*\Z')

# Heading styles
ATX = 'atx'
ATX_CLOSED = 'atx_closed'
Expand Down Expand Up @@ -446,6 +449,7 @@ def convert_a(self, el, text, parent_tags):
# For the replacement see #29: text nodes underscores are escaped
if (self.options['autolinks']
and text.replace(r'\_', '_') == href
and re_uri_autolink.match(href)
and not title
and not self.options['default_title']):
# Shortcut syntax
Expand Down
12 changes: 12 additions & 0 deletions tests/test_conversions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from markdownify import ATX, ATX_CLOSED, BACKSLASH, SPACES, UNDERSCORE
from .utils import md

Expand Down Expand Up @@ -36,6 +38,16 @@ def test_a_shortcut():
assert text == '<http://google.com>'


@pytest.mark.parametrize('href', ['section', '#section', '../page.html', '/docs', '//example.com', 'x:foo'])
def test_a_relative_destination_does_not_use_autolink(href):
assert md('<a href="%s">%s</a>' % (href, href)) == '[%s](%s)' % (href, href)


@pytest.mark.parametrize('href', ['https://example.com', 'ftp://example.com', 'custom+name:resource'])
def test_a_uri_destination_still_uses_autolink(href):
assert md('<a href="%s">%s</a>' % (href, href)) == '<%s>' % href


def test_a_no_autolinks():
assert md('<a href="https://google.com">https://google.com</a>', autolinks=False) == '[https://google.com](https://google.com)'

Expand Down