This extension for Python Markdown will convert text that look like links to HTML anchors.
There's an alternative package that serves the same purpose called
markdown-urlize
. The main
difference is that mdx_linkify
is
utilizing the excellent bleach
for
searching links in text. 👏
from markdown import markdown
markdown("minimal http://example.org/", extensions=["mdx_linkify"])
# Returns '<p>minimal <a href="http://example.org/">http://example.org/</a></p>'
It's possible to omit links that match your custom filter with linkify callbacks.
For example, to omit links that end with .txt
extension:
from mdx_linkify.mdx_linkify import LinkifyExtension
from markdown import Markdown
def dont_linkify_txt_extension(attrs, new=False):
if attrs["_text"].endswith(".txt"):
return None
return attrs
md = Markdown(
extensions=[LinkifyExtension()],
extension_configs={
"linkify": {
"linkify_callbacks": [[dont_linkify_txt_extension], ""]
}
}
)
assert md.convert("not_link.txt"), '<p>not_link.txt</p>'
expected = md.convert("example.com")
actual = '<p><a href="http://example.com">example.com</a></p>'
assert expected == actual
The project is on PyPI!
pip install mdx_linkify
If you want the bleeding-edge version (this includes unreleased-to-PyPI code), you can always grab the master branch directly from Git.
pip install git+git://github.com/daGrevis/mdx_linkify.git
git clone git@github.com:daGrevis/mdx_linkify.git
virtualenv mdx_linkify/
cd mdx_linkify/
source bin/activate
python setup.py install
python setup.py test
Pull requests are much welcome! 👍
(more like a cheatsheet for me actually)
- Change version in
setup.py
, - Commit and tag it,
- Push it (including tag),
- Run
python setup.py register && python setup.py sdist upload
;