Skip to content

A Python-Markdown extension to modify attributes of all <a> tag links. You can add target="_blank" attribute, control opener and referrer policy by adding related attributes, and add custom attributes.

License

Phuker/markdown_link_attr_modifier

Repository files navigation

markdown_link_attr_modifier

A Python-Markdown extension to modify attributes of <a> link tags. With this extension, you can:

For instance, this markdown code

[abc](https://example.com/)

could become

<p><a href="https://example.com/" referrerpolicy="no-referrer" rel="noopener noreferrer" target="_blank" title="abc">abc</a></p>

with this extension.

Support Python-Markdown 3.x. Tested against Python 3.9.1 + Python-Markdown 3.3.3. Not support Python < 3.6 (including 2.7.x) anymore since version 0.2.0.

Install

Before you start, check and upgrade your pip installation.

python3 -m pip install -U pip wheel setuptools

You can install this package with any of the methods below.

Install from PyPI

python3 -m pip install -U markdown-link-attr-modifier

Install from source code manually

First, clone/download this repo, and then:

make PYTHON=python3

make command required.

If you do NOT have make command:

python3 ./setup.py bdist_wheel
cd dist/

# For Windows CMD, press TAB just before press ENTER
python3 -m pip install *.whl

python3 -m pip show markdown_link_attr_modifier

Usage

import markdown

s = '[example](https://example.com/)'
extensions = ['markdown_link_attr_modifier', ]
extension_configs = {
    'markdown_link_attr_modifier': {
        'new_tab': 'on',
        'no_referrer': 'external_only',
        'auto_title': 'on',
    },
}

print(markdown.markdown(s, extensions=extensions, extension_configs=extension_configs))

Output:

<p><a href="https://example.com/" referrerpolicy="no-referrer" rel="noopener noreferrer" target="_blank" title="example">example</a></p>

You can also import manually:

import markdown
from markdown_link_attr_modifier import LinkAttrModifierExtension

s = '[example](https://example.com/)'

# without config
print(markdown.markdown(s, extensions=[LinkAttrModifierExtension()]))

# with config
print(markdown.markdown(s, extensions=[LinkAttrModifierExtension(new_tab='on', no_referrer='external_only')]))

Output:

<p><a href="https://example.com/">example</a></p>
<p><a href="https://example.com/" referrerpolicy="no-referrer" rel="noopener noreferrer" target="_blank">example</a></p>

For more information, see Extensions - Python-Markdown documentation and Using Markdown as a Python Library - Python-Markdown documentation.

CLI

python3 -m markdown -x markdown_link_attr_modifier input.md > output.html
python3 -m markdown -x markdown_link_attr_modifier -c config.json input.md > output.html

For more information, see Using Python-Markdown on the Command Line - Python-Markdown documentation.

Pelican

Pelican is a static site generator.

Edit pelicanconf.py, MARKDOWN dict variable. Example:

MARKDOWN = {
    'extension_configs': {
        'markdown.extensions.codehilite': {
            'css_class': 'highlight',
            'linenums': False,
            'guess_lang': False,
        },
        'markdown.extensions.extra': {},
        'markdown.extensions.meta': {},
        'markdown.extensions.toc': {},
        
        'markdown_link_attr_modifier': {
            'new_tab': 'on',
            'no_referrer': 'external_only',
            'auto_title': 'on',
        },
    },
    'output_format': 'html5',
}

For more information, see Settings - Pelican Docs.

Options

By default, this extension does NOT do anything. Configuration items:

  • new_tab: Open links in new tab, set target="_blank" and rel="noopener" attributes. Default value: 'off'. Valid values: ('on', 'external_only', 'off')
  • no_referrer: No referrer. Default value: 'off'. Valid values: ('on', 'external_only', 'off')
  • auto_title: Auto add title attribute. Default value: 'off'. Valid values: ('on', 'off')
  • custom_attrs: Custom attributes. Default value: {}. Valid value is a dict.

Values about how to match <a> links:

  • on: Modify all valid links.
  • external_only: Only modify external valid links.
  • off: Do not modify any link.

FYI:

Tests

Test installed module:

python3 -m markdown_link_attr_modifier -v

Test module in this repo without install:

make test

License

This repo is licensed under the GNU General Public License v3.0

About

A Python-Markdown extension to modify attributes of all <a> tag links. You can add target="_blank" attribute, control opener and referrer policy by adding related attributes, and add custom attributes.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published