A Python-Markdown extension to modify attributes of <a>
link tags. With this extension, you can:
- Open links in new tab by adding
target="_blank"
attribute (along withrel="noopener"
attribute for security reasons) - Do not send
Referer
(sic) request header by setting related attributes for privacy and security reasons - Auto add
title
attribute - Add/overwrite any custom attributes
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
.
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.
python3 -m pip install -U markdown-link-attr-modifier
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
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.
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 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.
By default, this extension does NOT do anything. Configuration items:
new_tab
: Open links in new tab, settarget="_blank"
andrel="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 adict
.
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:
Test installed module:
python3 -m markdown_link_attr_modifier -v
Test module in this repo without install:
make test
This repo is licensed under the GNU General Public License v3.0