Autolinks plugin for Patitas.
Enables GFM extended autolinks: bare URLs,www.links, and bare email
addresses are turned into links inside ordinary inline text. This implements
the GFM "Autolinks (extension)" rules:
https://github.github.com/gfm/#autolinks-extension-
>>> from patitas import Markdown
>>> Markdown(plugins=["autolinks"])("Visit https://example.com now")
'<p>Visit <a href="https://example.com">https://example.com</a> now</p>\n'
>>> Markdown(plugins=["autolinks"])("See www.example.com")
'<p>See <a href="http://www.example.com">www.example.com</a></p>\n'
>>> Markdown(plugins=["autolinks"])("Mail foo@example.com")
'<p>Mail <a href="mailto:foo@example.com">foo@example.com</a></p>\n'
Recognized forms (only when this plugin is enabled):
Rules applied (per the GFM extension):
- An autolink is recognized only at a left boundary: start-of-line/text,
after whitespace, or after one of
* _ ~ (.
- A URL/
www. body runs until whitespace or <, so characters that are
otherwise inline-special (& ~ $ _ * [ ! {) are kept as part of the link.
This means real-world URLs with query separators (?a=1&b=2), ~user
paths, andFoo_(bar)slugs link in full.
- Trailing punctuation (
? ! . , : * _ ~) is excluded from the link. A
trailing) is excluded when there are more ) than (in the match.
A trailing entity reference (&...;) is stripped.
- The authority must contain at least one
.and no spaces.
CommonMark angle-bracket autolinks work without this plugin:
>>> Markdown()("Visit <https://example.com>")
'<p>Visit <a href="https://example.com">https://example.com</a></p>\n'
Precedence (GFM: code spans > autolinks > emphasis):
- A backtick ends a URL body so a code span keeps its higher CommonMark/GFM
precedence (
http://a.com`code` does not swallow the code span).
- An
_inside an email local part is recognized as part of the address
before emphasis tokenization, so e.g.a_b@example.com links in full. (*
and~ are not valid email-local-part characters, so only _matters.)
Thread Safety:
This plugin is stateless and thread-safe.