Giter VIP home page Giter VIP logo

phuker / markdown_link_attr_modifier Goto Github PK

View Code? Open in Web Editor NEW
12.0 1.0 1.0 63 KB

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: GNU General Public License v3.0

Python 95.13% Makefile 4.87%
markdown python-markdown python

markdown_link_attr_modifier's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.