2020-07-17 16:50:57 +02:00
|
|
|
|
---
|
2022-03-27 14:01:30 +02:00
|
|
|
|
icon: material/code-json
|
2020-07-17 16:50:57 +02:00
|
|
|
|
---
|
|
|
|
|
|
2020-07-20 15:18:09 +02:00
|
|
|
|
# Code blocks
|
2020-07-17 16:50:57 +02: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 18:02:59 +02:00
|
|
|
|
highlighting for code blocks, either during build time using [Pygments] or
|
2020-07-17 16:50:57 +02:00
|
|
|
|
during runtime using a JavaScript syntax highlighter.
|
|
|
|
|
|
2021-10-03 18:02:59 +02:00
|
|
|
|
[Pygments]: https://pygments.org
|
2020-07-17 16:50:57 +02:00
|
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
2021-10-03 18:02:59 +02: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
|
2021-10-04 23:36:31 +02:00
|
|
|
|
following lines to `mkdocs.yml`:
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
markdown_extensions:
|
2021-12-10 13:31:08 +01:00
|
|
|
|
- pymdownx.highlight:
|
|
|
|
|
anchor_linenums: true
|
2020-07-19 22:23:26 +02:00
|
|
|
|
- pymdownx.inlinehilite
|
2021-10-03 18:02:59 +02:00
|
|
|
|
- pymdownx.snippets
|
2021-12-10 13:31:08 +01:00
|
|
|
|
- pymdownx.superfences
|
2020-07-19 22:23:26 +02:00
|
|
|
|
```
|
|
|
|
|
|
2021-10-30 13:16:05 +02:00
|
|
|
|
The following sections discuss how to use different syntax highlighting features
|
|
|
|
|
with [Pygments], the recommended highlighter, so they don't apply when using a
|
|
|
|
|
JavaScript syntax highlighter.
|
|
|
|
|
|
2021-10-03 18:02:59 +02:00
|
|
|
|
See additional configuration options:
|
2020-07-21 13:33:44 +02:00
|
|
|
|
|
2021-10-03 18:02:59 +02:00
|
|
|
|
- [Highlight]
|
|
|
|
|
- [InlineHilite]
|
|
|
|
|
- [SuperFences]
|
|
|
|
|
- [Snippets]
|
2020-07-21 13:33:44 +02:00
|
|
|
|
|
2021-10-03 18:02:59 +02: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 13:33:44 +02:00
|
|
|
|
|
2023-01-02 14:29:16 +01:00
|
|
|
|
### Code copy button
|
|
|
|
|
|
|
|
|
|
[:octicons-tag-24: 9.0.0][Code copy button support] ·
|
|
|
|
|
:octicons-unlock-24: Feature flag
|
|
|
|
|
|
|
|
|
|
Code blocks can automatically render a button on the right side to allow the
|
|
|
|
|
user to copy a code block's contents to the clipboard. Add the following to
|
|
|
|
|
`mkdocs.yml` to enable them globally:
|
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
theme:
|
|
|
|
|
features:
|
|
|
|
|
- content.code.copy
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
[Code copy button support]: https://github.com/squidfunk/mkdocs-material/releases/tag/9.0.0
|
|
|
|
|
|
|
|
|
|
??? info "Enabling or disabling code copy buttons for a specific code block"
|
|
|
|
|
|
|
|
|
|
If you don't want to enable code copy buttons globally, you can enable them
|
|
|
|
|
for a specific code block by using a slightly different syntax based on the
|
|
|
|
|
[Attribute Lists] extension:
|
|
|
|
|
|
|
|
|
|
```` yaml
|
|
|
|
|
``` { .yaml .copy }
|
|
|
|
|
# Code block content
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
Note that the language shortcode which has to come first must now also be
|
2023-02-16 16:21:11 +01:00
|
|
|
|
prefixed by a `.`. Similarly, the copy button can also be disabled for a
|
2023-01-02 14:29:16 +01:00
|
|
|
|
specific code block:
|
|
|
|
|
|
|
|
|
|
```` { .yaml .no-copy }
|
|
|
|
|
``` { .yaml .no-copy }
|
|
|
|
|
# Code block content
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
2021-10-03 18:02:59 +02:00
|
|
|
|
### Code annotations
|
2020-07-23 15:34:43 +02:00
|
|
|
|
|
2021-11-28 15:59:54 +01:00
|
|
|
|
[:octicons-tag-24: 8.0.0][Code annotations support] ·
|
2022-06-02 17:20:33 +02:00
|
|
|
|
:octicons-unlock-24: Feature flag
|
2020-07-23 15:34:43 +02:00
|
|
|
|
|
2021-10-03 18:02:59 +02: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
|
2021-10-04 23:36:31 +02:00
|
|
|
|
and inline comments in the language of the code block. Add the following to
|
2021-10-03 18:02:59 +02:00
|
|
|
|
`mkdocs.yml` to enable them globally:
|
2020-07-21 13:33:44 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
2021-10-03 18:02:59 +02:00
|
|
|
|
theme:
|
|
|
|
|
features:
|
2021-12-11 14:30:07 +01:00
|
|
|
|
- content.code.annotate # (1)!
|
2020-07-21 13:33:44 +02:00
|
|
|
|
```
|
|
|
|
|
|
2021-10-03 18:02:59 +02:00
|
|
|
|
1. :man_raising_hand: I'm a code annotation! I can contain `code`, __formatted
|
2022-01-08 11:26:00 +01:00
|
|
|
|
text__, images, ... basically anything that can be written in Markdown.
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2021-10-04 23:36:31 +02:00
|
|
|
|
??? info "Enabling code annotations for a specific code block"
|
2020-07-22 11:57:41 +02:00
|
|
|
|
|
2021-10-03 18:02:59 +02: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
|
2021-10-10 12:19:14 +02:00
|
|
|
|
[Attribute Lists] extension:
|
2020-07-22 11:57:41 +02:00
|
|
|
|
|
2021-10-03 18:02:59 +02: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 11:57:41 +02:00
|
|
|
|
|
2021-11-28 15:59:54 +01:00
|
|
|
|
[Code annotations support]: https://github.com/squidfunk/mkdocs-material/releases/tag/8.0.0
|
2021-10-10 12:19:14 +02:00
|
|
|
|
[Attribute Lists]: ../setup/extensions/python-markdown.md#attribute-lists
|
2020-07-22 11:57:41 +02:00
|
|
|
|
|
2021-12-10 10:12:07 +01:00
|
|
|
|
#### Anchor links
|
|
|
|
|
|
2022-09-13 13:27:13 +02:00
|
|
|
|
[:octicons-tag-24: 8.5.0][Anchor links support] ·
|
2021-12-10 10:12:07 +01:00
|
|
|
|
:octicons-beaker-24: Experimental
|
|
|
|
|
|
2022-09-18 18:17:30 +02:00
|
|
|
|
In order to be able to link to code annotations and share them more easily, an
|
2022-09-13 13:27:13 +02:00
|
|
|
|
anchor link is automatically added to each annotation, which you can copy via
|
|
|
|
|
right click or open in a new tab:
|
2021-12-10 10:12:07 +01:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
# (1)!
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
1. If you ++cmd++ :material-plus::material-cursor-default-outline: me, I'm
|
|
|
|
|
rendered open in a new tab. You can also right-click me to __copy link
|
|
|
|
|
address__ to share me with others.
|
|
|
|
|
|
2022-09-13 13:27:13 +02:00
|
|
|
|
[Anchor links support]: https://github.com/squidfunk/mkdocs-material/releases/tag/8.5.0
|
2021-12-10 10:12:07 +01:00
|
|
|
|
|
2020-07-19 22:23:26 +02:00
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
Code blocks must be enclosed with two separate lines containing three backticks.
|
2021-10-03 18:02:59 +02: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
|
2022-01-10 14:31:58 +01:00
|
|
|
|
shortcode for a given language:
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
```` markdown title="Code block"
|
2021-11-21 17:45:49 +01:00
|
|
|
|
``` py
|
2020-07-19 22:23:26 +02:00
|
|
|
|
import tensorflow as tf
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
<div class="result" markdown>
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2021-11-21 17:45:49 +01:00
|
|
|
|
``` py
|
2020-07-19 22:23:26 +02:00
|
|
|
|
import tensorflow as tf
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
2021-10-03 18:02:59 +02:00
|
|
|
|
[list of available lexers]: https://pygments.org/docs/lexers/
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2021-10-30 13:16:05 +02:00
|
|
|
|
### Adding a title
|
|
|
|
|
|
|
|
|
|
In order to provide additional context, a custom title can be added to a code
|
|
|
|
|
block by using the `title="<custom title>"` option directly after the shortcode,
|
|
|
|
|
e.g. to display the name of a file:
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
```` markdown title="Code block with title"
|
2021-10-30 13:16:05 +02:00
|
|
|
|
``` py title="bubble_sort.py"
|
|
|
|
|
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]
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
<div class="result" markdown>
|
2021-10-30 13:16:05 +02:00
|
|
|
|
|
|
|
|
|
``` py title="bubble_sort.py"
|
|
|
|
|
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]
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
2021-02-28 14:23:11 +01:00
|
|
|
|
### Adding annotations
|
|
|
|
|
|
2021-10-03 18:02:59 +02: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
|
2022-01-10 14:31:58 +01:00
|
|
|
|
`#!js /* ... */`, for YAML in `#!yaml # ...`, etc.[^1]:
|
2021-02-28 14:23:11 +01:00
|
|
|
|
|
2021-10-03 18:02:59 +02:00
|
|
|
|
[^1]:
|
|
|
|
|
Code annotations require syntax highlighting with [Pygments] – they're
|
2021-11-07 18:23:01 +01:00
|
|
|
|
currently not compatible with JavaScript syntax highlighters, or languages
|
|
|
|
|
that do not have comments in their grammar. However, we're actively working
|
|
|
|
|
on supporting alternate ways of defining code annotations, allowing to
|
|
|
|
|
always place code annotations at the end of lines.
|
2021-02-28 14:23:11 +01:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
```` markdown title="Code block with annotation"
|
2021-07-18 17:57:45 +02:00
|
|
|
|
``` yaml
|
|
|
|
|
theme:
|
|
|
|
|
features:
|
2021-10-03 18:02:59 +02:00
|
|
|
|
- content.code.annotate # (1)
|
2021-02-28 14:23:11 +01:00
|
|
|
|
```
|
|
|
|
|
|
2021-10-03 18:02:59 +02:00
|
|
|
|
1. :man_raising_hand: I'm a code annotation! I can contain `code`, __formatted
|
2022-01-08 11:26:00 +01:00
|
|
|
|
text__, images, ... basically anything that can be written in Markdown.
|
2021-02-28 14:23:11 +01:00
|
|
|
|
````
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
<div class="result" markdown>
|
2021-02-28 14:23:11 +01:00
|
|
|
|
|
2021-10-03 18:02:59 +02:00
|
|
|
|
``` yaml
|
|
|
|
|
theme:
|
|
|
|
|
features:
|
|
|
|
|
- content.code.annotate # (1)
|
2021-06-15 11:03:15 +02:00
|
|
|
|
```
|
|
|
|
|
|
2021-10-03 18:02:59 +02:00
|
|
|
|
1. :man_raising_hand: I'm a code annotation! I can contain `code`, __formatted
|
2022-01-08 11:26:00 +01:00
|
|
|
|
text__, images, ... basically anything that can be written in Markdown.
|
2021-02-28 14:23:11 +01:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
2021-12-10 10:12:07 +01:00
|
|
|
|
#### Stripping comments
|
|
|
|
|
|
2022-09-13 13:27:13 +02:00
|
|
|
|
[:octicons-tag-24: 8.5.0][Stripping comments support] ·
|
2021-12-10 10:12:07 +01:00
|
|
|
|
:octicons-beaker-24: Experimental
|
|
|
|
|
|
|
|
|
|
If you wish to strip the comment characters surrounding a code annotation,
|
2022-09-13 13:27:13 +02:00
|
|
|
|
simply add an `!` after the closing parenthesis of the code annotation:
|
2021-12-10 10:12:07 +01:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
```` markdown title="Code block with annotation, stripped"
|
2021-12-10 10:12:07 +01:00
|
|
|
|
``` yaml
|
|
|
|
|
# (1)!
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
1. Look ma, less line noise!
|
|
|
|
|
````
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
<div class="result" markdown>
|
2021-12-10 10:12:07 +01:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
# (1)!
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
1. Look ma, less line noise!
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
2021-12-10 10:12:07 +01:00
|
|
|
|
Note that this only allows for a single code annotation to be rendered per
|
|
|
|
|
comment. If you want to add multiple code annotations, comments cannot be
|
|
|
|
|
stripped for technical reasons.
|
|
|
|
|
|
2022-09-13 13:27:13 +02:00
|
|
|
|
[Stripping comments support]: https://github.com/squidfunk/mkdocs-material/releases/tag/8.5.0
|
|
|
|
|
|
2020-07-19 22:23:26 +02:00
|
|
|
|
### Adding line numbers
|
|
|
|
|
|
|
|
|
|
Line numbers can be added to a code block by using the `linenums="<start>"`
|
2021-10-03 18:02:59 +02:00
|
|
|
|
option directly after the shortcode, whereas `<start>` represents the starting
|
2020-07-19 22:23:26 +02:00
|
|
|
|
line number. A code block can start from a line number other than `1`, which
|
2022-01-10 14:31:58 +01:00
|
|
|
|
allows to split large code blocks for readability:
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
```` markdown title="Code block with line numbers"
|
2021-11-21 17:45:49 +01:00
|
|
|
|
``` py linenums="1"
|
2020-07-19 22:23:26 +02:00
|
|
|
|
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]
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
<div class="result" markdown>
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2021-11-21 17:45:49 +01:00
|
|
|
|
``` py linenums="1"
|
2020-07-19 22:23:26 +02:00
|
|
|
|
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]
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
2020-07-19 22:23:26 +02:00
|
|
|
|
### Highlighting specific lines
|
|
|
|
|
|
|
|
|
|
Specific lines can be highlighted by passing the line numbers to the `hl_lines`
|
2021-10-03 18:02:59 +02: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
|
2022-01-10 14:31:58 +01:00
|
|
|
|
[`linenums`][Adding line numbers]:
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
```` markdown title="Code block with highlighted lines"
|
|
|
|
|
``` py 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 14:19:42 +02:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
<div class="result" markdown>
|
2021-08-02 14:19:42 +02:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
``` py linenums="1" 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-10-03 18:02:59 +02:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
</div>
|
2021-10-03 18:02:59 +02:00
|
|
|
|
|
|
|
|
|
[Adding line numbers]: #adding-line-numbers
|
2021-08-02 14:19:42 +02:00
|
|
|
|
|
2020-07-22 11:57:41 +02:00
|
|
|
|
### Highlighting inline code blocks
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2021-10-03 18:02:59 +02: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 22:23:26 +02:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
``` markdown title="Inline code block"
|
2020-07-19 22:23:26 +02:00
|
|
|
|
The `#!python range()` function is used to generate a sequence of numbers.
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
<div class="result" markdown>
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
|
|
|
|
The `#!python range()` function is used to generate a sequence of numbers.
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
2020-07-23 17:00:20 +02:00
|
|
|
|
### Embedding external files
|
2020-07-23 15:34:43 +02:00
|
|
|
|
|
2021-10-04 23:36:31 +02:00
|
|
|
|
When [Snippets] is enabled, content from other files (including source files)
|
|
|
|
|
can be embedded by using the [`--8<--` notation][Snippets notation] directly
|
|
|
|
|
from within a code block:
|
2020-07-23 15:34:43 +02:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
```` markdown title="Code block with external content"
|
2021-10-30 13:16:05 +02:00
|
|
|
|
``` title=".browserslistrc"
|
2022-09-20 18:54:54 +02:00
|
|
|
|
--8<-- ".browserslistrc"
|
2020-07-23 15:34:43 +02:00
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
<div class="result" markdown>
|
2020-07-23 15:34:43 +02:00
|
|
|
|
|
2021-10-30 13:16:05 +02:00
|
|
|
|
``` title=".browserslistrc"
|
2021-01-12 17:14:41 +01:00
|
|
|
|
last 4 years
|
2020-07-23 15:34:43 +02:00
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
2021-10-04 23:36:31 +02:00
|
|
|
|
[Snippets notation]: https://facelessuser.github.io/pymdown-extensions/extensions/snippets/#snippets-notation
|
|
|
|
|
|
2020-07-23 17:00:20 +02:00
|
|
|
|
## Customization
|
|
|
|
|
|
|
|
|
|
### Custom syntax theme
|
|
|
|
|
|
2021-10-03 18:02:59 +02:00
|
|
|
|
If [Pygments] is used, Material for MkDocs provides the [styles for code blocks]
|
2021-10-10 12:19:14 +02:00
|
|
|
|
[colors], which are built with a custom and well-balanced palette that works
|
2021-10-03 18:02:59 +02:00
|
|
|
|
equally well for both [color schemes]:
|
2020-07-24 14:30:03 +02:00
|
|
|
|
|
2021-03-13 14:30:29 +01: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 14:30:03 +02:00
|
|
|
|
|
2020-08-02 12:37:01 +02:00
|
|
|
|
Code block foreground, background and line highlight colors are defined via:
|
|
|
|
|
|
2021-03-13 14:30:29 +01: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 12:37:01 +02:00
|
|
|
|
|
2020-07-24 14:30:03 +02:00
|
|
|
|
Let's say you want to change the color of `#!js "strings"`. While there are
|
2021-10-03 18:02:59 +02:00
|
|
|
|
several [types of string tokens], they use the same color. You can assign
|
2021-10-10 12:19:14 +02:00
|
|
|
|
a new color by using an [additional style sheet]:
|
2020-07-24 14:30:03 +02:00
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
=== ":octicons-file-code-16: `docs/stylesheets/extra.css`"
|
2021-10-10 12:19:14 +02:00
|
|
|
|
|
|
|
|
|
``` css
|
|
|
|
|
:root > * {
|
|
|
|
|
--md-code-hl-string-color: #0FF1CE;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
=== ":octicons-file-code-16: `mkdocs.yml`"
|
2021-10-10 12:19:14 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
extra_css:
|
2021-10-10 17:39:53 +02:00
|
|
|
|
- stylesheets/extra.css
|
2021-10-10 12:19:14 +02:00
|
|
|
|
```
|
2020-07-24 14:30:03 +02:00
|
|
|
|
|
2021-10-04 23:36:31 +02:00
|
|
|
|
If you want to tweak a specific type of string, e.g. ``#!js `backticks` ``, you
|
|
|
|
|
can lookup the specific CSS class name in the [syntax theme definition], and
|
2021-10-10 12:19:14 +02:00
|
|
|
|
override it as part of your [additional style sheet]:
|
2020-07-24 14:30:03 +02:00
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
=== ":octicons-file-code-16: `docs/stylesheets/extra.css`"
|
2021-10-10 12:19:14 +02:00
|
|
|
|
|
|
|
|
|
``` css
|
|
|
|
|
.highlight .sb {
|
|
|
|
|
color: #0FF1CE;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
=== ":octicons-file-code-16: `mkdocs.yml`"
|
2021-10-10 12:19:14 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
extra_css:
|
2021-10-10 17:39:53 +02:00
|
|
|
|
- stylesheets/extra.css
|
2021-10-10 12:19:14 +02:00
|
|
|
|
```
|
2020-07-23 17:00:20 +02:00
|
|
|
|
|
2021-10-10 12:19:14 +02:00
|
|
|
|
[colors]: https://github.com/squidfunk/mkdocs-material/blob/master/src/assets/stylesheets/main/_colors.scss
|
2021-10-03 18:02:59 +02:00
|
|
|
|
[color schemes]: ../setup/changing-the-colors.md#color-scheme
|
|
|
|
|
[types of string tokens]: https://pygments.org/docs/tokens/#literals
|
2021-10-10 12:19:14 +02:00
|
|
|
|
[additional style sheet]: ../customization.md#additional-css
|
2021-10-03 18:02:59 +02:00
|
|
|
|
[syntax theme definition]: https://github.com/squidfunk/mkdocs-material/blob/master/src/assets/stylesheets/main/extensions/pymdownx/_highlight.scss
|
2021-12-10 10:12:07 +01:00
|
|
|
|
|
2022-04-01 17:55:19 +02:00
|
|
|
|
### Annotation tooltip width
|
|
|
|
|
|
|
|
|
|
If you have a lot of content hosted inside your code annotations, it can be a
|
|
|
|
|
good idea to increase the width of the tooltip by adding the following as part
|
|
|
|
|
of an [additional style sheet]:
|
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
=== ":octicons-file-code-16: `docs/stylesheets/extra.css`"
|
2022-04-01 17:55:19 +02:00
|
|
|
|
|
|
|
|
|
``` css
|
|
|
|
|
:root {
|
|
|
|
|
--md-tooltip-width: 600px;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
=== ":octicons-file-code-16: `mkdocs.yml`"
|
2022-04-01 17:55:19 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
extra_css:
|
|
|
|
|
- stylesheets/extra.css
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This will render annotations with a larger width:
|
|
|
|
|
|
|
|
|
|
<div style="--md-tooltip-width: 600px;" markdown>
|
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
# (1)!
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
1. Muuuuuuuuuuuuuuuch more space for content
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
2021-12-10 10:12:07 +01:00
|
|
|
|
### Annotations with numbers
|
|
|
|
|
|
|
|
|
|
Prior to [:octicons-tag-24: 8.1.0][code annotation markers], code annotations
|
|
|
|
|
were rendered with markers showing the original number as used by the author.
|
|
|
|
|
However, for technical reasons code annotation numbers restart each code block,
|
|
|
|
|
which might lead to confusion. For this reason, code annotations now render as
|
|
|
|
|
`+` signs which are rotated if they're open to denote that clicking them again
|
|
|
|
|
will close them.
|
|
|
|
|
|
|
|
|
|
If you wish to revert to the prior behavior and display code annotation numbers,
|
|
|
|
|
you can add an [additional style sheet] and copy and paste the following CSS:
|
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
=== ":octicons-file-code-16: `docs/stylesheets/extra.css`"
|
2021-12-10 10:12:07 +01:00
|
|
|
|
|
|
|
|
|
``` css
|
|
|
|
|
.md-typeset .md-annotation__index > ::before {
|
|
|
|
|
content: attr(data-md-annotation-id);
|
|
|
|
|
}
|
|
|
|
|
.md-typeset :focus-within > .md-annotation__index > ::before {
|
|
|
|
|
transform: none;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
=== ":octicons-file-code-16: `mkdocs.yml`"
|
2021-12-10 10:12:07 +01:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
extra_css:
|
|
|
|
|
- stylesheets/extra.css
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
[code annotation markers]: https://github.com/squidfunk/mkdocs-material/releases/tag/8.1.0
|