2020-07-17 17:50:57 +03:00
|
|
|
|
---
|
|
|
|
|
template: overrides/main.html
|
|
|
|
|
---
|
|
|
|
|
|
2020-07-20 16:18:09 +03:00
|
|
|
|
# Code blocks
|
2020-07-17 17:50:57 +03:00
|
|
|
|
|
|
|
|
|
Code blocks and examples are an essential part of technical project
|
|
|
|
|
documentation. Material for MkDocs provides different ways to set up syntax
|
2021-10-03 19:02:59 +03:00
|
|
|
|
highlighting for code blocks, either during build time using [Pygments] or
|
2020-07-17 17:50:57 +03:00
|
|
|
|
during runtime using a JavaScript syntax highlighter.
|
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
[Pygments]: https://pygments.org
|
2020-07-17 17:50:57 +03:00
|
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
This configuration enables syntax highlighting on code blocks and inline code
|
|
|
|
|
blocks, and allows to include source code directly from other files. Add the
|
|
|
|
|
following lines to `mkdocs.yml`
|
2020-07-19 23:23:26 +03:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
markdown_extensions:
|
2021-10-03 19:02:59 +03:00
|
|
|
|
- pymdownx.highlight
|
2020-07-19 23:23:26 +03:00
|
|
|
|
- pymdownx.inlinehilite
|
2021-10-03 19:02:59 +03:00
|
|
|
|
- pymdownx.superfences
|
|
|
|
|
- pymdownx.snippets
|
2020-07-19 23:23:26 +03:00
|
|
|
|
```
|
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
See additional configuration options:
|
2020-07-21 14:33:44 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
- [Highlight]
|
|
|
|
|
- [InlineHilite]
|
|
|
|
|
- [SuperFences]
|
|
|
|
|
- [Snippets]
|
2020-07-21 14:33:44 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
[Highlight]: ../setup/extensions/python-markdown-extensions.md#highlight
|
|
|
|
|
[InlineHilite]: ../setup/extensions/python-markdown-extensions.md#inlinehilite
|
|
|
|
|
[SuperFences]: ../setup/extensions/python-markdown-extensions.md#superfences
|
|
|
|
|
[Snippets]: ../setup/extensions/python-markdown-extensions.md#snippets
|
2020-07-21 14:33:44 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
### Code annotations
|
2020-07-23 16:34:43 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
[:octicons-heart-fill-24:{ .mdx-heart } Insiders][Insiders]{ .mdx-insiders } ·
|
2021-10-03 19:29:50 +03:00
|
|
|
|
:octicons-unlock-24: Feature flag ·
|
2021-10-03 19:02:59 +03:00
|
|
|
|
:octicons-beaker-24: Experimental ·
|
|
|
|
|
[:octicons-tag-24: insiders-2.2.0 ... present][Insiders]
|
2020-07-23 16:34:43 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
Code annotations offer a comfortable and friendly way to attach arbitrary
|
|
|
|
|
content to specific sections of code blocks by adding numeric markers in block
|
|
|
|
|
and inline comments in the language of the block. Add the following to
|
|
|
|
|
`mkdocs.yml` to enable them globally:
|
2020-07-21 14:33:44 +03:00
|
|
|
|
|
|
|
|
|
``` yaml
|
2021-10-03 19:02:59 +03:00
|
|
|
|
theme:
|
|
|
|
|
features:
|
|
|
|
|
- content.code.annotate # (1)
|
2020-07-21 14:33:44 +03:00
|
|
|
|
```
|
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
1. :man_raising_hand: I'm a code annotation! I can contain `code`, __formatted
|
|
|
|
|
text__, images, ... basically anything that can be expressed in Markdown.
|
2020-07-19 23:23:26 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
??? info "Enabling code annotations only for specific code blocks"
|
2020-07-22 12:57:41 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
If you don't want to enable code annotations globally, because you don't
|
|
|
|
|
like the automatic inlining behavior, you can enable them for a specific
|
|
|
|
|
code block by using a slightly different syntax based on the
|
|
|
|
|
[Attribute List] extension:
|
2020-07-22 12:57:41 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
```` yaml
|
|
|
|
|
``` { .yaml .annotate }
|
|
|
|
|
# Code block content
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
Note that the language shortcode which has to come first must now also be
|
|
|
|
|
prefixed by a `.`.
|
2020-07-22 12:57:41 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
[Insiders]: ../insiders/index.md
|
|
|
|
|
[Attribute List]: ../setup/extensions/python-markdown.md#attribute-lists
|
2020-07-22 12:57:41 +03:00
|
|
|
|
|
2020-07-19 23:23:26 +03:00
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
This section discusses how to use different syntax highlighting features with
|
2021-10-03 19:02:59 +03:00
|
|
|
|
[Pygments] – the default highlighter – so they don't apply when using
|
2020-07-20 18:40:48 +03:00
|
|
|
|
a JavaScript syntax highlighter.
|
2020-07-19 23:23:26 +03:00
|
|
|
|
|
|
|
|
|
### Specifying the language
|
|
|
|
|
|
|
|
|
|
Code blocks must be enclosed with two separate lines containing three backticks.
|
2021-10-03 19:02:59 +03:00
|
|
|
|
To add syntax highlighting to those blocks, add the language shortcode directly
|
|
|
|
|
after the opening block. See the [list of available lexers] to find the
|
|
|
|
|
shortcode for a given language.
|
2020-07-19 23:23:26 +03:00
|
|
|
|
|
|
|
|
|
_Example_:
|
|
|
|
|
|
|
|
|
|
```` markdown
|
|
|
|
|
``` python
|
|
|
|
|
import tensorflow as tf
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
_Result_:
|
|
|
|
|
|
|
|
|
|
``` python
|
|
|
|
|
import tensorflow as tf
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
[list of available lexers]: https://pygments.org/docs/lexers/
|
2020-07-19 23:23:26 +03:00
|
|
|
|
|
2021-02-28 16:23:11 +03:00
|
|
|
|
### Adding annotations
|
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
Code annotations can be placed anywhere in a code block where a comment for the
|
|
|
|
|
language of the block can be placed, e.g. for JavaScript in `#!js // ...` and
|
|
|
|
|
`#!js /* ... */`, for Yaml `#!yaml # ...`, etc.[^1]
|
2021-02-28 16:23:11 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
[^1]:
|
|
|
|
|
Code annotations require syntax highlighting with [Pygments] – they're
|
|
|
|
|
currently not compatible with JavaScript syntax highlighters. Support will
|
|
|
|
|
be added at a later point, allowing to always place code annotations at the
|
|
|
|
|
end of lines.
|
2021-02-28 16:23:11 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
_Example_:
|
2021-07-18 18:57:45 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
```` markdown
|
2021-07-18 18:57:45 +03:00
|
|
|
|
``` yaml
|
|
|
|
|
theme:
|
|
|
|
|
features:
|
2021-10-03 19:02:59 +03:00
|
|
|
|
- content.code.annotate # (1)
|
2021-02-28 16:23:11 +03:00
|
|
|
|
```
|
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
1. :man_raising_hand: I'm a code annotation! I can contain `code`, __formatted
|
|
|
|
|
text__, images, ... basically anything that can be expressed in Markdown.
|
2021-02-28 16:23:11 +03:00
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
_Result_:
|
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
``` yaml
|
|
|
|
|
theme:
|
|
|
|
|
features:
|
|
|
|
|
- content.code.annotate # (1)
|
2021-06-15 12:03:15 +03:00
|
|
|
|
```
|
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
1. :man_raising_hand: I'm a code annotation! I can contain `code`, __formatted
|
|
|
|
|
text__, images, ... basically anything that can be expressed in Markdown.
|
2021-02-28 16:23:11 +03:00
|
|
|
|
|
2020-07-19 23:23:26 +03:00
|
|
|
|
### Adding line numbers
|
|
|
|
|
|
|
|
|
|
Line numbers can be added to a code block by using the `linenums="<start>"`
|
2021-10-03 19:02:59 +03:00
|
|
|
|
option directly after the shortcode, whereas `<start>` represents the starting
|
2020-07-19 23:23:26 +03:00
|
|
|
|
line number. A code block can start from a line number other than `1`, which
|
2021-10-03 19:02:59 +03:00
|
|
|
|
allows to split large code blocks for readability.
|
2020-07-19 23:23:26 +03:00
|
|
|
|
|
|
|
|
|
_Example_:
|
|
|
|
|
|
|
|
|
|
```` markdown
|
|
|
|
|
``` python linenums="1"
|
|
|
|
|
def bubble_sort(items):
|
|
|
|
|
for i in range(len(items)):
|
|
|
|
|
for j in range(len(items) - 1 - i):
|
|
|
|
|
if items[j] > items[j + 1]:
|
|
|
|
|
items[j], items[j + 1] = items[j + 1], items[j]
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
_Result_:
|
|
|
|
|
|
|
|
|
|
``` python linenums="1"
|
|
|
|
|
def bubble_sort(items):
|
|
|
|
|
for i in range(len(items)):
|
|
|
|
|
for j in range(len(items) - 1 - i):
|
|
|
|
|
if items[j] > items[j + 1]:
|
|
|
|
|
items[j], items[j + 1] = items[j + 1], items[j]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Highlighting specific lines
|
|
|
|
|
|
|
|
|
|
Specific lines can be highlighted by passing the line numbers to the `hl_lines`
|
2021-10-03 19:02:59 +03:00
|
|
|
|
argument placed right after the language shortcode. Note that line counts start
|
|
|
|
|
at `1`, regardless of the starting line number specified as part of
|
|
|
|
|
[`linenums`][Adding line numbers].
|
2020-07-19 23:23:26 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
=== "Line numbers"
|
2020-07-19 23:23:26 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
_Example_:
|
2020-07-19 23:23:26 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
```` markdown
|
|
|
|
|
``` python hl_lines="2 3"
|
|
|
|
|
def bubble_sort(items):
|
|
|
|
|
for i in range(len(items)):
|
|
|
|
|
for j in range(len(items) - 1 - i):
|
|
|
|
|
if items[j] > items[j + 1]:
|
|
|
|
|
items[j], items[j + 1] = items[j + 1], items[j]
|
|
|
|
|
```
|
|
|
|
|
````
|
2020-07-19 23:23:26 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
_Result_:
|
2020-07-19 23:23:26 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
``` python hl_lines="2 3"
|
|
|
|
|
def bubble_sort(items):
|
|
|
|
|
for i in range(len(items)):
|
|
|
|
|
for j in range(len(items) - 1 - i):
|
|
|
|
|
if items[j] > items[j + 1]:
|
|
|
|
|
items[j], items[j + 1] = items[j + 1], items[j]
|
|
|
|
|
```
|
2021-08-02 15:19:42 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
=== "Line ranges"
|
2021-08-02 15:19:42 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
_Example_:
|
2021-08-02 15:19:42 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
```` markdown
|
|
|
|
|
``` python hl_lines="2-5"
|
|
|
|
|
def bubble_sort(items):
|
|
|
|
|
for i in range(len(items)):
|
|
|
|
|
for j in range(len(items) - 1 - i):
|
|
|
|
|
if items[j] > items[j + 1]:
|
|
|
|
|
items[j], items[j + 1] = items[j + 1], items[j]
|
|
|
|
|
```
|
|
|
|
|
````
|
2021-08-02 15:19:42 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
_Result_:
|
|
|
|
|
|
|
|
|
|
``` python hl_lines="2-5"
|
|
|
|
|
def bubble_sort(items):
|
|
|
|
|
for i in range(len(items)):
|
|
|
|
|
for j in range(len(items) - 1 - i):
|
|
|
|
|
if items[j] > items[j + 1]:
|
|
|
|
|
items[j], items[j + 1] = items[j + 1], items[j]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
[Adding line numbers]: #adding-line-numbers
|
2021-08-02 15:19:42 +03:00
|
|
|
|
|
2020-07-22 12:57:41 +03:00
|
|
|
|
### Highlighting inline code blocks
|
2020-07-19 23:23:26 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
When [InlineHilite] is enabled, syntax highlighting can be applied to inline
|
|
|
|
|
code blocks by prefixing them with a shebang, i.e. `#!`, directly followed by
|
|
|
|
|
the corresponding [language shortcode][list of available lexers].
|
2020-07-19 23:23:26 +03:00
|
|
|
|
|
|
|
|
|
_Example_:
|
|
|
|
|
|
2020-07-24 10:59:16 +03:00
|
|
|
|
``` markdown
|
2020-07-19 23:23:26 +03:00
|
|
|
|
The `#!python range()` function is used to generate a sequence of numbers.
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
_Result_:
|
|
|
|
|
|
|
|
|
|
The `#!python range()` function is used to generate a sequence of numbers.
|
|
|
|
|
|
2020-07-23 18:00:20 +03:00
|
|
|
|
### Embedding external files
|
2020-07-23 16:34:43 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
When [Snippets] is enabled, content from other files can be embedded, which is particularly useful to reference and embed the contents of source files
|
|
|
|
|
directly in a document without copying.
|
2020-07-23 16:34:43 +03:00
|
|
|
|
|
|
|
|
|
_Example_:
|
|
|
|
|
|
|
|
|
|
```` markdown
|
|
|
|
|
```
|
|
|
|
|
--8<-- ".browserslistrc"
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
_Result_:
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-12 19:14:41 +03:00
|
|
|
|
last 4 years
|
2020-07-23 16:34:43 +03:00
|
|
|
|
```
|
|
|
|
|
|
2020-07-23 18:00:20 +03:00
|
|
|
|
## Customization
|
|
|
|
|
|
|
|
|
|
### Custom syntax theme
|
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
[:octicons-file-code-24: Source][Source] ·
|
2020-07-23 18:00:20 +03:00
|
|
|
|
:octicons-mortar-board-24: Difficulty: _easy_
|
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
If [Pygments] is used, Material for MkDocs provides the [styles for code blocks]
|
|
|
|
|
[Source], which are built with a custom and well-balanced palette that works
|
|
|
|
|
equally well for both [color schemes]:
|
2020-07-24 15:30:03 +03:00
|
|
|
|
|
2021-03-13 16:30:29 +03:00
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-hl-number-color) " } `--md-code-hl-number-color`
|
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-hl-special-color) " } `--md-code-hl-special-color`
|
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-hl-function-color) " } `--md-code-hl-function-color`
|
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-hl-constant-color) " } `--md-code-hl-constant-color`
|
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-hl-keyword-color) " } `--md-code-hl-keyword-color`
|
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-hl-string-color) " } `--md-code-hl-string-color`
|
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-hl-name-color) " } `--md-code-hl-name-color`
|
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-hl-operator-color) " } `--md-code-hl-operator-color`
|
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-hl-punctuation-color) " } `--md-code-hl-punctuation-color`
|
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-hl-comment-color) " } `--md-code-hl-comment-color`
|
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-hl-generic-color) " } `--md-code-hl-generic-color`
|
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-hl-variable-color) " } `--md-code-hl-variable-color`
|
2020-07-24 15:30:03 +03:00
|
|
|
|
|
2020-08-02 13:37:01 +03:00
|
|
|
|
Code block foreground, background and line highlight colors are defined via:
|
|
|
|
|
|
2021-03-13 16:30:29 +03:00
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-fg-color) " } `--md-code-fg-color`
|
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-bg-color) " } `--md-code-bg-color`
|
|
|
|
|
- :material-checkbox-blank-circle:{ style="color: var(--md-code-hl-color) " } `--md-code-hl-color`
|
2020-08-02 13:37:01 +03:00
|
|
|
|
|
2020-07-24 15:30:03 +03:00
|
|
|
|
Let's say you want to change the color of `#!js "strings"`. While there are
|
2021-10-03 19:02:59 +03:00
|
|
|
|
several [types of string tokens], they use the same color. You can assign
|
|
|
|
|
a new color by using an [additional stylesheet]:
|
2020-07-24 15:30:03 +03:00
|
|
|
|
|
|
|
|
|
``` css
|
2020-07-29 17:47:22 +03:00
|
|
|
|
:root > * {
|
2020-07-24 15:30:03 +03:00
|
|
|
|
--md-code-hl-string-color: #0FF1CE;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
If you want to tweak a specific type of string, i.e. ``#!js `backticks` ``, you
|
2021-10-03 19:02:59 +03:00
|
|
|
|
can lookup the specific class name in the [syntax theme definition], and
|
2020-07-24 15:30:03 +03:00
|
|
|
|
override it as part of your additional stylesheet:
|
|
|
|
|
|
|
|
|
|
``` css
|
|
|
|
|
.highlight .sb {
|
|
|
|
|
color: #0FF1CE;
|
|
|
|
|
}
|
|
|
|
|
```
|
2020-07-23 18:00:20 +03:00
|
|
|
|
|
2021-10-03 19:02:59 +03:00
|
|
|
|
[Source]: https://github.com/squidfunk/mkdocs-material/blob/master/src/assets/stylesheets/main/_colors.scss
|
|
|
|
|
[color schemes]: ../setup/changing-the-colors.md#color-scheme
|
|
|
|
|
[types of string tokens]: https://pygments.org/docs/tokens/#literals
|
|
|
|
|
[additional stylesheet]: ../customization.md#additional-css
|
|
|
|
|
[syntax theme definition]: https://github.com/squidfunk/mkdocs-material/blob/master/src/assets/stylesheets/main/extensions/pymdownx/_highlight.scss
|