Skip to main content

Link recognition for Python Markdown

Project description

# Mdx Linkify

[![Build Status](https://travis-ci.org/daGrevis/mdx_linkify.png?branch=master)](https://travis-ci.org/daGrevis/mdx_linkify)
[![Coverage Status](https://coveralls.io/repos/daGrevis/mdx_linkify/badge.png?branch=master)](https://coveralls.io/r/daGrevis/mdx_linkify?branch=master)
[![PyPI Downloads](https://pypip.in/d/mdx_linkify/badge.png)](https://pypi.python.org/pypi/mdx_linkify)
[![PyPI Version](https://pypip.in/v/mdx_linkify/badge.png)](https://pypi.python.org/pypi/mdx_linkify)

This extension for [Python Markdown](https://github.com/waylan/Python-Markdown)
will convert all links to HTML anchors.

There's [an existing solution](https://github.com/r0wb0t/markdown-urlize) for
parsing links with regexes. Mdx Linkify is a bit smarter and asks
[Bleach](https://github.com/jsocol/bleach) to parse them. :clap:

## Usage

### Basic Example

By default, if you add `linkify` extension to `markdown` extensions...

```python
from markdown import markdown


text = "http://example.org/"

assert markdown(text) == "<p>http://example.org/</p>"

expected = markdown(text, extensions=["linkify"])
actual = '<p><a href="http://example.org/">http://example.org/</a></p>'
assert expected == actual
```

...it will automatically convert all links to HTML anchors.

### Linkify Callbacks

If you need callbacks, you can specify them in `extension_configs` like shown
below.

```python
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
```

...here, we only convert links that do **not** end with `.txt` extension.

## Installation

The project is [on PyPI](https://pypi.python.org/pypi/mdx_linkify)!

pip install mdx_linkify

## Development

```
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! :+1:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mdx_linkify-0.5.tar.gz (3.3 kB view details)

Uploaded Source

File details

Details for the file mdx_linkify-0.5.tar.gz.

File metadata

  • Download URL: mdx_linkify-0.5.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mdx_linkify-0.5.tar.gz
Algorithm Hash digest
SHA256 4f52f57a011ca6c3f593e298e34766a8b629e756938b60b20ff41da6fda98b34
MD5 3d09fba79495b2e67d803a4e929028be
BLAKE2b-256 af544425b352de6240ba50b7c8b798c21dac43d740072ffb6fd6b2ae759c9b56

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page