You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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.
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?
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?
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.
Activity
waylan commentedon Apr 4, 2019
The answer is mostly the same as it was for #179. The one difference is that
safe_modewas 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.
geoffroy-noel-ddh commentedon Apr 24, 2019
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?
waylan commentedon Apr 24, 2019
@geoffroy-noel-ddh yes, the Extension API can alter any part of the parser.
geoffroy-noel-ddh commentedon Apr 25, 2019
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?
waylan commentedon Apr 25, 2019
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 domd.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.Markdownclass 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.geoffroy-noel-ddh commentedon Apr 25, 2019
@waylan thanks for taking the time to reply, that's very useful.