Skip to content

Disable parts of markdown syntax #811

Closed

Description

@tapia

I know this question has been asked before in #179, but it was 6 years ago, so I think it's worth a try asking again.

I'd like to disable some parts of the markdown syntax in my app. In particular, but not only, I'd like to disable the embedded HTML. Is it possible somehow?

Activity

  1. waylan commented on Apr 4, 2019

    @waylan
    Member

    The answer is mostly the same as it was for #179. The one difference is that safe_mode was fully deprecated in version 3.0. In fact, the release notes provide a few suggested alternatives for dealing with raw HTML. I expect you'll find what you are looking for there.

    Finally, I will note that we recommend using an HTML sanitizer (like Bleach) rather than disabling raw HTML, as disabling raw HTML does not make Markdown secure. You still need a sanitizer if you are parsing Markdown from untrusted sources. See Markdown and XSS for an analysis of the security vulnerabilities of Markdown syntax itself. While that article is written by the developer of PHP Markdown and concentrates on PHP solutions, the principles apply equally to Python-Markdown.

    If you are looking to disable parts of the Markdown syntax for reasons other than security, then the Extension API is available to alter anything you would like. For the foreseeable future, the Extension API will be the only way to alter the behavior of the parser.

  2. added
    support
    on Apr 4, 2019
  3. geoffroy-noel-ddh commented on Apr 24, 2019

    @geoffroy-noel-ddh

    Hi, I have a related question. Is there a way to selectively enable core constructs. For instance I would like to convert links and headings but not blockquotes or emphasis. Is that possible?

  4. waylan commented on Apr 24, 2019

    @waylan
    Member

    @geoffroy-noel-ddh yes, the Extension API can alter any part of the parser.

  5. geoffroy-noel-ddh commented on Apr 25, 2019

    @geoffroy-noel-ddh

    Do you have more specific examples of that? I can see how to add extensions but I'm not sure how to remove all the core syntax and add them one at a time. Say if I want to start with just links (and perhaps add other core constructs later), what's the approach? Is it as simple as passing a list of names/constants or would I have to override a lot of functions and reuse the built-in patterns?

  6. waylan commented on Apr 25, 2019

    @waylan
    Member

    You might find this tutorial in the wiki to be helpful. However, be aware that it has not yet been updated for version 3.x. Regardless, the basic concepts still stand.

    The primary difference between 2.x and 3.x is that 3.x uses the new Registry. So, rather than using del md.inlinePatterns['strong_em'] to remove a processor as demonstrated in the tutorial, you would do md.inlinePatterns.deregister('strong_em').

    Of course, if you wanted to rewrite most of the parser, you might find it more efficient to subclass the markdown.Markdown class and override the build_parser method. In this way, you can build up the parts you want, rather than having the default parser built up, only to tear it back down.

  7. geoffroy-noel-ddh commented on Apr 25, 2019

    @geoffroy-noel-ddh

    @waylan thanks for taking the time to reply, that's very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    support

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions