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