Skip to content
Merged
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
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ See the [Contributing Guide](contributing.md) for details.

### Fixed

* Support automatic links with URL schemes other than HTTP and FTP, such as
`<ssh://example.org>` (#1244).
* Fix quadratic-time id generation in the `toc` extension when many headers
share the same text.
* Strip complete HTML tags with quoted `>` characters from `toc` labels and
Expand Down
7 changes: 7 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ are summarized below:
foreseeable future. That said, the [Sane List Extension](extensions/sane_lists.md)
is available to provide a less surprising behavior.

* __Automatic Links__

Automatic links recognize URL schemes followed by `://`, including schemes
other than HTTP and FTP, such as `<ssh://example.org>` and
`<git+ssh://example.org/repo>`. URI forms without `://`, such as
`<urn:foo>`, continue to be treated as raw HTML.

Support
-------

Expand Down
2 changes: 1 addition & 1 deletion markdown/inlinepatterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def build_inlinepatterns(md: Markdown, **kwargs: Any) -> util.Registry[InlinePro
IMAGE_REFERENCE_RE = IMAGE_LINK_RE
""" Match start of image reference (`![alt text][2]`). """

AUTOLINK_RE = r'<((?:[Ff]|[Hh][Tt])[Tt][Pp][Ss]?://[^<>]*)>'
AUTOLINK_RE = r'<([A-Za-z][A-Za-z0-9+.-]*://[^<>]*)>'
""" Match an automatic link (`<http://www.example.com>`). """

AUTOMAIL_RE = r'<([^<> !]+@[^@<> ]+)>'
Expand Down
66 changes: 66 additions & 0 deletions tests/test_syntax/inline/test_autolinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,72 @@

class TestAutomaticLinks(TestCase):

def test_url_schemes(self):
for url in (
'http://example.org',
'https://example.org',
'ftp://example.org',
'ftps://example.org',
'HtTpS://example.org',
'ssh://example.org',
'git://example.org/repo',
's3://bucket/key',
'git+ssh://example.org/repo',
'x.y-z://example.org',
):
with self.subTest(url=url):
self.assertMarkdownRenders(
f'<{url}>',
f'<p><a href="{url}">{url}</a></p>'
)

def test_url_schemes_with_userinfo(self):
for url in (
'ssh://[email protected]/path',
'git+ssh://[email protected]/repo.git',
):
with self.subTest(url=url):
self.assertMarkdownRenders(
f'<{url}>',
f'<p><a href="{url}">{url}</a></p>'
)

def test_url_scheme_with_query(self):
self.assertMarkdownRenders(
'<ssh://example.org/path?q=one&x=two>',
'<p><a href="ssh://example.org/path?q=one&amp;x=two">'
'ssh://example.org/path?q=one&amp;x=two</a></p>'
)

def test_adjacent_url_schemes(self):
self.assertMarkdownRenders(
'Before <ssh://one><git://two> after',
'<p>Before <a href="ssh://one">ssh://one</a>'
'<a href="git://two">git://two</a> after</p>'
)

def test_url_scheme_without_closing_bracket(self):
self.assertMarkdownRenders(
'<ssh://example.org',
'<p>&lt;ssh://example.org</p>'
)

def test_url_scheme_in_code_span(self):
self.assertMarkdownRenders(
'`<ssh://example.org>`',
'<p><code>&lt;ssh://example.org&gt;</code></p>'
)

def test_invalid_url_schemes(self):
for source, expected in (
('<1http://example.org>', '<p>&lt;1http://example.org&gt;</p>'),
('<git_ssh://example.org>', '<p><git_ssh://example.org></p>'),
('<urn:foo>', '<p><urn:foo></p>'),
('<em>boo!</em>', '<p><em>boo!</em></p>'),
):
with self.subTest(source=source):
self.assertMarkdownRenders(source, expected)

def test_email_address(self):
self.assertMarkdownRenders(
'asdfasdfadsfasd <[email protected]> or you can say ',
Expand Down
Loading