Disabling images in Python Markdown

Here’s a quick snippet that shows how to disable images (or any extension) in Python Markdown:

import markdown

class MyMarkdownExtension(markdown.Extension):

	def extendMarkdown(self, md, md_globals):
		del md.inlinePatterns['image_link']
		del md.inlinePatterns['image_reference']

my_markdown_extension = MyMarkdownExtension()

raw_text = 'This is a test image: ![Google Logo](http://www.google.com/images/logos/ps_logo2a_cp.png)'

print markdown.markdown(raw_text, [my_markdown_extension], safe_mode = 'escape')

Edit:
Some processing is disabled through md.parser.blockprocessors instead.

Here are the options for each:
md.inlinePatterns:

'backtick'
'escape'
'reference'
'link'
'image_link'
'image_reference'
'short_reference'
'autolink'
'automail'
'linebreak2'
'linebreak'
'html'
'entity'
'not_strong'
'strong_em'
'strong'
'emphasis'
'emphasis2'

md.parser.blockprocessors:

'indent'
'code'
'hashheader'
'setextheader'
'hr'
'olist'
'ulist'
'quote'
'paragraph'
About these ads

5 thoughts on “Disabling images in Python Markdown

  1. Just a little remark:
    On python-markdown 2.0.3 you should create Markdown object to pass your own extension.


    md = markdown.Markdown(extensions=[my_markdown_extension], safe_mode='escape')
    print md.convert(raw_text)

  2. Thank you very muxh, it was useful. But I would like to disable more tags of markdown.
    I want a version extended and another one minified.
    The extended would be the normal markdown, but in the minified I don’t want to let code, hr, lists, quote, paragraphs.. you know how to disable them? It would be really useful. Please help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s