title: Fenced Code Blocks Extension

Fenced Code Blocks


The Fenced Code Blocks extension adds a secondary way to define code blocks, which overcomes a few limitations of the indented code blocks.

This extension is included in the standard Markdown library.


Fenced Code Blocks are defined using the syntax established in PHP Markdown Extra.

Thus, the following text (taken from the above referenced PHP documentation):

This is a paragraph introducing:

a one-line code block

Fenced code blocks can have a blank line as the first and/or last line of a code block and they can also come immediately after a list item without becoming part of the list.

!!! warning

Fenced Code Blocks are only supported at the document root level.
Therefore, they cannot be nested inside lists or blockquotes.


In addition to PHP Extra's syntax, you can define the language of the code block for use by syntax highlighters etc. The language will be assigned as a class attribute of the <code> element in the output. Therefore, you should define the language as you would a CSS class - .language. For consistency with other markdown syntax, the language can optionally be wrapped in curly brackets:

# python code

<p>HTML Document</p>

The above will output:

<pre><code class="python"># python code

<pre><code class="html">&lt;p&gt;HTML Document&lt;/p&gt;

GitHub's backtick (```) syntax is also supported in this extension:

# more python code

Emphasized Lines

If you would like to have your fenced code blocks highlighted with the CodeHilite extension, simply enable that extension (remember that Pygments is its dependency) and the language of your fenced code blocks will be passed in and highlighted appropriately.

Similar to the colon syntax of the CodeHilite extension, fenced code blocks can also have emphasized certain lines of code.

The lines can be specified with PHP Extra's syntax:

~~~~{.python hl_lines="1 3"}
# This line is emphasized
# This line isn't
# This line is emphasized

... or with GitHub's:

```python hl_lines="1 3"
# This line is emphasized
# This line isn't
# This line is emphasized


See Extensions for general extension usage. Use fenced_code as the name of the extension.

This extension does not accept any special configuration options.

A trivial example:

markdown.markdown(some_text, extensions=['fenced_code'])