After googling for a while for a plugin to automatically add rel="nofollow"
and target="_blank"
attributes to all html a
tags for my Django blog, nothing was found but this one called md_nofollow by bhch.
Unfortunately, this plugin doesn't work for my blog, maybe its because of the version of python markdown. So I decided to write my own one.
python markdown 3.x.x, see here for installation
since the APIs are different with versions of python markdown, so this is important.
the name of this plugin is python_markdown_nofollow
, you can easily install this plugin by command:
pip install python-markdown-nofollow
here is a detailed example of how to use this plugin for your django blog.
in the frontend, we use a template tag to translate markdown syntax to html like:
{{ blog.content|std_markdown|safe }}
in my app directory, I create a directory called templatetags
:
├── app
│ ├── templatetags
│ ├── std_markdown.py
and under templatetags
directory we create a file called std_markdown.py
in the std_markdown.py
file we are going to do the translation from markdown to html:
from django import template
from django.template.defaultfilters import stringfilter
import markdown # import python markdown
register = template.Library()
@register.filter()
@stringfilter
def std_markdown(value):
return markdown.markdown(value, extensions=[
'markdown.extensions.fenced_code',
])
after doing this, our front will work well with a link looks like (use the inspect of a browers):
<a href='https://stdworkflow.com'>external link</a>
now we integrate python_markdown_nofollow to our translator by simply adding:
def std_markdown(value):
return markdown.markdown(value, extensions=[
'python_markdown_nofollow',
...
])
restart our project, in our front end we will see:
<a href='https://stdworkflow.com' rel="external nofollow" target="_blank">external link</a>
well, if you have learnt something about SEO, you may know it is good to add rel="nofollow"
attribute for external links in our websites, which would be good for the SEO of our site.
Since the targets of this plugin are all the <a>
tags in our content, which may include links of our own site, so external nofollow
rather than nofollow
was used.
this plugin was released on: