1title: Fenced Code Blocks Extension 2 3# Fenced Code Blocks 4 5## Summary 6 7The Fenced Code Blocks extension adds a secondary way to define code blocks, which overcomes a few limitations of 8indented code blocks. 9 10This extension is included in the standard Markdown library. 11 12## Syntax 13 14Fenced Code Blocks are defined using the syntax originally established in [PHP Markdown Extra][php] and popularized by 15[GitHub Flavored Markdown][gfm]. 16 17Fenced code blocks begin with three or more backticks (` ``` `) or tildes (`~~~`) on a line by themselves and end with 18a matching set of backticks or tildes on a line by themselves. The closing set must contain the same number and type 19of characters as the opening set. It is recommended that a blank line be placed before and after the code block. 20 21````md 22A paragraph before the code block. 23 24``` 25a one-line code block 26``` 27 28A paragraph after the code block. 29```` 30 31While backticks seem to be more popular among users, tildes may be used as well. 32 33````md 34~~~ 35a one-line code block 36~~~ 37```` 38 39To include a set of backticks (or tildes) within a code block, use a different number of backticks for the 40deliminators. 41 42`````md 43```` 44``` 45```` 46````` 47 48Fenced code blocks can have a blank line as the first and/or last line of the code block and those lines will be 49preserved. 50 51````md 52``` 53 54a three-line code block 55 56``` 57```` 58 59Unlike indented code blocks, a fenced code block can immediately follow a list item without becoming 60part of the list. 61 62````md 63* A list item. 64 65``` 66not part of the list 67``` 68```` 69 70!!! warning 71 72 Fenced Code Blocks are only supported at the document root level. Therefore, they cannot be nested inside lists or 73 blockquotes. If you need to nest fenced code blocks, you may want to try the third party extension [SuperFences] 74 instead. 75 76### Attributes 77 78Various attributes may be defined on a per-code-block basis by defining them immediately following the opening 79deliminator. The attributes should be wrapped in curly braces `{}` and be on the same line as the deliminator. It is 80generally best to separate the attribute list from the deliminator with a space. Attributes within the list must be 81separated by a space. 82 83````md 84``` { attributes go here } 85a code block with attributes 86``` 87```` 88 89How those attributes will affect the output will depend on various factors as described below. 90 91#### Language 92 93The language of the code within a code block can be specified for use by syntax highlighters, etc. The language should 94be prefixed with a dot and not contain any whitespace (`.language-name`). 95 96````md 97``` { .html } 98<p>HTML Document</p> 99``` 100```` 101 102So long as the language is the only option specified, the curly brackets and/or the dot may be excluded: 103 104````md 105``` html 106<p>HTML Document</p> 107``` 108```` 109 110Either of the above examples will output the following HTML: 111 112```html 113<pre><code class="language-html"><p>HTML Document</p> 114</code></pre> 115``` 116 117Note that the language name has been prefixed with `language-` and it has been assigned to the `class` attribute on 118the `<code>` tag, which is the format suggested by the [HTML 5 Specification][html5] (see the second "example" in the 119Specification). While `language` is the default prefix, the prefix may be overridden using the 120[`lang_prefix`](#lang_prefix) configuration option. 121 122#### Classes 123 124In addition to the language, additional classes may be defined by prefixing them with a dot, just like the language. 125 126````md 127``` { .html .foo .bar } 128<p>HTML Document</p> 129``` 130```` 131 132When defining multiple classes, only the first class will be used as the "language" for the code block. All others are 133assigned to the `<pre>` tag unaltered. Additionally, the curly braces and dot are required for all classes, including 134the language class if more than one class is defined. 135 136The above example will output the following HTML: 137 138```html 139<pre class="foo bar"><code class="language-html"><p>HTML Document</p> 140</code></pre> 141``` 142 143#### ID 144 145An `id` can be defined for a code block, which would allow a link to point directly to the code block using a URL 146hash. IDs must be prefixed with a hash character (`#`) and only contain characters permitted in HTML `id` attributes. 147 148````md 149``` { #example } 150A linkable code block 151``` 152```` 153 154The `id` attribute is assigned to the `<pre>` tag of the output. The above example will output the following HTML: 155 156```html 157<pre id="example"><code>A linkable code block 158</code></pre> 159``` 160 161From elsewhere within the same document, one could link to the code block with `[link](#example)`. 162 163IDs may be defined along with the language, other classes, or any other supported attributes. The order of items does 164not matter. 165 166````md 167``` { #example .lang .foo .bar } 168A linkable code block 169``` 170```` 171 172#### Key/Value Pairs 173 174If the `fenced_code` and [`attr_list`][attr_list] extensions are both enabled, then key/value pairs can be defined in 175the attribute list. So long as code highlighting is not enabled (see below), the key/value pairs will be assigned as 176attributes on the `<code>` tag in the output. Key/value pairs must be defined using the syntax documented for the 177`attr_list` extension (for example, values with whitespace must be wrapped in quotes). 178 179````md 180``` { .lang #example style="color: #333; background: #f8f8f8;" } 181A code block with inline styles. Fancy! 182``` 183```` 184 185The above example will output the following HTML: 186 187```html 188<pre id="example"><code class="language-lang" style="color: #333; background: #f8f8f8;">A code block with inline styles. Fancy! 189</code></pre> 190``` 191 192If the `attr_list` extension is not enabled, then the key/value pairs will be ignored. 193 194#### Syntax Highlighting 195 196If the `fenced_code` extension and syntax highlighting are both enabled, then the [`codehilite`][codehilite] 197extension will be used for syntax highlighting the contents of the code block. The language defined in the attribute 198list will be passed to `codehilite` to ensure that the correct language is used. If no language is specified and 199language guessing is not disabled for the `codehilite` extension, then the language will be guessed. 200 201The `codehilite` extension uses the [Pygments] engine to do syntax highlighting. Any valid Pygments options can be 202defined as key/value pairs in the attribute list and will be passed on to Pygments. 203 204````md 205``` { .lang linenos=true linenostart=42 hl_lines="43-44 50" title="An Example Code Block" }` 206A truncated code block... 207``` 208```` 209 210Valid options include any option accepted by Pygments' [`HTMLFormatter`][HTMLFormatter] except for the `full` option, 211as well as any options accepted by the relevant [lexer][lexer] (each language has its own lexer). While most lexers 212don't have options that are all that useful in this context, there are a few important exceptions. For example, the 213[PHP lexer's] `startinline` option eliminates the need to start each code fragment with `<?php`. 214 215!!! note 216 217 The `fenced_code` extension does not alter the output provided by Pygments. Therefore, only options which Pygments 218 provides can be utilized. As Pygments does not currently provide a way to define an ID, any ID defined in an 219 attribute list will be ignored when syntax highlighting is enabled. Additionally, any key/value pairs which are not Pygments options will be ignored, regardless of whether the `attr_list` extension is enabled. 220 221##### Enabling Syntax Highlighting 222 223To enable syntax highlighting, the [`codehilite`][codehilite] extension must be enabled and the `codehilite` 224extension's `use_pygments` option must be set to `True` (the default). 225 226Alternatively, so long as the `codehilite` extension is enabled, you can override a global `use_pygments=False` 227option for an individual code block by including `use_pygments=true` in the attribute list. While the `use_pygments` 228key/value pair will not be included in the output, all other attributes will behave as they would if syntax 229highlighting was enabled only for that code block. 230 231Conversely, to disable syntax highlighting on an individual code block, include `use_pygments=false` in the attribute 232list. While the `use_pygments` key/value pair will not be included in the output, all other attributes will behave as 233they would if syntax highlighting was disabled for that code block regardless of any global setting. 234 235!!! seealso "See Also" 236 237 You will need to properly install and setup Pygments for syntax highlighting to work. See the `codehilite` 238 extension's [documentation][setup] for details. 239 240## Usage 241 242See [Extensions] for general extension usage. Use `fenced_code` as the name of the extension. 243 244See the [Library Reference] for information about configuring extensions. 245 246The following option is provided to configure the output: 247 248* **`lang_prefix`**{#lang_prefix}: 249 The prefix prepended to the language class assigned to the HTML `<code>` tag. Default: `language-`. 250 251A trivial example: 252 253```python 254markdown.markdown(some_text, extensions=['fenced_code']) 255``` 256 257[php]: http://www.michelf.com/projects/php-markdown/extra/#fenced-code-blocks 258[gfm]: https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks 259[SuperFences]: https://facelessuser.github.io/pymdown-extensions/extensions/superfences/ 260[html5]: https://html.spec.whatwg.org/#the-code-element 261[attr_list]: ./attr_list.md 262[codehilite]: ./code_hilite.md 263[Pygments]: http://pygments.org/ 264[HTMLFormatter]: https://pygments.org/docs/formatters/#HtmlFormatter 265[lexer]: https://pygments.org/docs/lexers/ 266[PHP lexer's]: https://pygments.org/docs/lexers/#lexers-for-php-and-related-languages 267[setup]: ./code_hilite.md#setup 268[Extensions]: ./index.md 269[Library Reference]: ../reference.md#extensions 270