简体   繁体   中英

limit Markdown in Django

I am using markdown on the comments system in my blog developed using django I want to limit the possible format to accept just a basic one (with bold, italic, link and code )

How do I set Markdown to do this?

if this is not possible using markdown so any alternatives? PS: i am using the default django app 'django.contrib.markup'

here is the actual code i am using on my template:

          <div class="comment-content>
            <p>
             {% load markup %}
             {{ comment.comment|markdown:"safe" }}
            </p>
          </div>

You could use Bleach and write a template tag to strip out the tags you don't want.

For example, to allow only bold and italic:

@register.filter
def limit_markdown(comment):
    comment = bleach.clean(comment, tags=['b', 'i', 'em'], strip=True)
    return comment

Then in your template, you could use it as:

{{ comment.comment|markdown|limit_markdown|safe }}

It would depend on which markdown plugin you're using there are many out there from a quick google search.

You'll have to either find documentation online for the specific one you are using, or perhaps look through the source and if it's open source modify it if you have to. Or just find another one that allows that functionality.

edit: Seems that django uses python-markdown(http://www.freewisdom.org/projects/python-markdown/), from a quick look it doesn't seem to support specifying only specific formatting options. However it seems to be easily extensible, so if you write an extension you can use it in django like this:

{{ string|markdown:"extension_name,extension2,etc..." }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM