| 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 |
## Syntax |
| 13 |
|
| 14 |
Fenced Code Blocks are defined using the syntax originally established in [PHP Markdown Extra][php] and popularized by |
| 15 |
[GitHub Flavored Markdown][gfm]. |
| 16 |
|
| 17 |
Fenced code blocks begin with three or more backticks (` ``` `) or tildes (`~~~`) on a line by themselves and end with |
| 18 |
a matching set of backticks or tildes on a line by themselves. The closing set must contain the same number and type |
| 19 |
of characters as the opening set. It is recommended that a blank line be placed before and after the code block. |
| 20 |
|
| 21 |
````md |
| 22 |
A paragraph before the code block. |
| 23 |
|
| 24 |
``` |
| 25 |
a one-line code block |
| 26 |
``` |
| 27 |
|
| 28 |
A paragraph after the code block. |
| 29 |
```` |
| 30 |
|
| 31 |
While backticks seem to be more popular among users, tildes may be used as well. |
| 32 |
|
| 33 |
````md |
| 34 |
~~~ |
| 35 |
a one-line code block |
| 36 |
~~~ |
| 37 |
```` |
| 38 |
|
| 39 |
To include a set of backticks (or tildes) within a code block, use a different number of backticks for the |
| 40 |
delimiters. |
| 41 |
|
| 42 |
`````md |
| 43 |
```` |
| 44 |
``` |
| 45 |
```` |
| 46 |
````` |
| 47 |
|
| 48 |
Fenced code blocks can have a blank line as the first and/or last line of the code block and those lines will be |
| 49 |
preserved. |
| 50 |
|
| 51 |
````md |
| 52 |
``` |
| 53 |
|
| 54 |
a three-line code block |
| 55 |
|
| 56 |
``` |
| 57 |
```` |
| 58 |
|
| 59 |
Unlike indented code blocks, a fenced code block can immediately follow a list item without becoming |
| 60 |
part of the list. |
| 61 |
|
| 62 |
````md |
| 63 |
* A list item. |
| 64 |
|
| 65 |
``` |
| 66 |
not 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 |
|
| 78 |
Various attributes may be defined on a per-code-block basis by defining them immediately following the opening |
| 79 |
deliminator. The attributes should be wrapped in curly braces `{}` and be on the same line as the deliminator. It is |
| 80 |
generally best to separate the attribute list from the deliminator with a space. Attributes within the list must be |
| 81 |
separated by a space. |
| 82 |
|
| 83 |
````md |
| 84 |
``` { attributes go here } |
| 85 |
a code block with attributes |
| 86 |
``` |
| 87 |
```` |
| 88 |
|
| 89 |
How those attributes will affect the output will depend on various factors as described below. |
| 90 |
|
| 91 |
#### Language |
| 92 |
|
| 93 |
The language of the code within a code block can be specified for use by syntax highlighters, etc. The language should |
| 94 |
be prefixed with a dot and not contain any whitespace (`.language-name`). |
| 95 |
|
| 96 |
````md |
| 97 |
``` { .html } |
| 98 |
<p>HTML Document</p> |
| 99 |
``` |
| 100 |
```` |
| 101 |
|
| 102 |
So 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 |
|
| 110 |
Either 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 |
|
| 117 |
Note that the language name has been prefixed with `language-` and it has been assigned to the `class` attribute on |
| 118 |
the `<code>` tag, which is the format suggested by the [HTML 5 Specification][html5] (see the second "example" in the |
| 119 |
Specification). While `language` is the default prefix, the prefix may be overridden using the |
| 120 |
[`lang_prefix`](#lang_prefix) configuration option. |
| 121 |
|
| 122 |
#### Classes |
| 123 |
|
| 124 |
In 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 |
|
| 132 |
When defining multiple classes, only the first class will be used as the "language" for the code block. All others are |
| 133 |
assigned to the `<pre>` tag unaltered. Additionally, the curly braces and dot are required for all classes, including |
| 134 |
the language class if more than one class is defined. |
| 135 |
|
| 136 |
The 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 |
|
| 145 |
An `id` can be defined for a code block, which would allow a link to point directly to the code block using a URL |
| 146 |
hash. IDs must be prefixed with a hash character (`#`) and only contain characters permitted in HTML `id` attributes. |
| 147 |
|
| 148 |
````md |
| 149 |
``` { #example } |
| 150 |
A linkable code block |
| 151 |
``` |
| 152 |
```` |
| 153 |
|
| 154 |
The `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 |
|
| 161 |
From elsewhere within the same document, one could link to the code block with `[link](#example)`. |
| 162 |
|
| 163 |
IDs may be defined along with the language, other classes, or any other supported attributes. The order of items does |
| 164 |
not matter. |
| 165 |
|
| 166 |
````md |
| 167 |
``` { #example .lang .foo .bar } |
| 168 |
A linkable code block |
| 169 |
``` |
| 170 |
```` |
| 171 |
|
| 172 |
#### Key/Value Pairs |
| 173 |
|
| 174 |
If the `fenced_code` and [`attr_list`][attr_list] extensions are both enabled, then key/value pairs can be defined in |
| 175 |
the attribute list. So long as code highlighting is not enabled (see below), the key/value pairs will be assigned as |
| 176 |
attributes 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;" } |
| 181 |
A code block with inline styles. Fancy! |
| 182 |
``` |
| 183 |
```` |
| 184 |
|
| 185 |
The 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 |
|
| 192 |
If the `attr_list` extension is not enabled, then the key/value pairs will be ignored. |
| 193 |
|
| 194 |
#### Syntax Highlighting |
| 195 |
|
| 196 |
If the `fenced_code` extension and syntax highlighting are both enabled, then the [`codehilite`][codehilite] |
| 197 |
extension will be used for syntax highlighting the contents of the code block. The language defined in the attribute |
| 198 |
list will be passed to `codehilite` to ensure that the correct language is used. If no language is specified and |
| 199 |
language guessing is not disabled for the `codehilite` extension, then the language will be guessed. |
| 200 |
|
| 201 |
The `codehilite` extension uses the [Pygments] engine to do syntax highlighting. Any valid Pygments options can be |
| 202 |
defined 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" }` |
| 206 |
A truncated code block... |
| 207 |
``` |
| 208 |
```` |
| 209 |
|
| 210 |
Valid options include any option accepted by Pygments' [`HTMLFormatter`][HTMLFormatter] except for the `full` option, |
| 211 |
as well as any options accepted by the relevant [lexer][lexer] (each language has its own lexer). While most lexers |
| 212 |
don'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 |
|
| 223 |
To enable syntax highlighting, the [`codehilite`][codehilite] extension must be enabled and the `codehilite` |
| 224 |
extension's `use_pygments` option must be set to `True` (the default). |
| 225 |
|
| 226 |
Alternatively, so long as the `codehilite` extension is enabled, you can override a global `use_pygments=False` |
| 227 |
option for an individual code block by including `use_pygments=true` in the attribute list. While the `use_pygments` |
| 228 |
key/value pair will not be included in the output, all other attributes will behave as they would if syntax |
| 229 |
highlighting was enabled only for that code block. |
| 230 |
|
| 231 |
Conversely, to disable syntax highlighting on an individual code block, include `use_pygments=false` in the attribute |
| 232 |
list. While the `use_pygments` key/value pair will not be included in the output, all other attributes will behave as |
| 233 |
they 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 |
|
| 242 |
See [Extensions] for general extension usage. Use `fenced_code` as the name of the extension. |
| 243 |
|
| 244 |
See the [Library Reference] for information about configuring extensions. |
| 245 |
|
| 246 |
The 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 |
|
| 251 |
A trivial example: |
| 252 |
|
| 253 |
```python |
| 254 |
markdown.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 |