2020-07-17 16:50:57 +02:00
|
|
|
|
---
|
|
|
|
|
template: overrides/main.html
|
|
|
|
|
---
|
|
|
|
|
|
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
|
|
|
|
|
highlighting for code blocks, either during build time using [Pygments][1] or
|
|
|
|
|
during runtime using a JavaScript syntax highlighter.
|
|
|
|
|
|
|
|
|
|
[1]: https://pygments.org
|
|
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
|
|
### Highlight
|
|
|
|
|
|
2020-07-21 13:33:44 +02:00
|
|
|
|
[:octicons-file-code-24: Source][2] · [:octicons-workflow-24: Extension][3] ·
|
|
|
|
|
:octicons-zap-24: Supersedes: [CodeHilite][4]
|
2020-07-17 16:50:57 +02:00
|
|
|
|
|
2020-07-21 13:33:44 +02:00
|
|
|
|
The [Highlight][3] extension, which is part of [Python Markdown Extensions][5],
|
2020-07-17 16:50:57 +02:00
|
|
|
|
integrates with Material for MkDocs and provides several options for
|
|
|
|
|
configuring syntax highlighting of code blocks:
|
|
|
|
|
|
2020-07-23 17:00:20 +02:00
|
|
|
|
`use_pygments`{: #use-pygments }
|
2020-07-17 16:50:57 +02:00
|
|
|
|
|
2020-07-21 16:01:22 +02:00
|
|
|
|
: :octicons-milestone-24: Default: `true` – This option allows to control
|
2020-07-20 15:18:09 +02:00
|
|
|
|
whether highlighting should be carried out during build time by
|
|
|
|
|
[Pygments][1] or runtime with a JavaScript highlighter. Remember to add the
|
2020-07-21 13:33:44 +02:00
|
|
|
|
necessary [additional stylesheets][6] and [JavaScript][7] if you want to
|
2020-07-20 15:18:09 +02:00
|
|
|
|
use the latter:
|
2020-07-17 16:50:57 +02:00
|
|
|
|
|
|
|
|
|
=== "Pygments"
|
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
markdown_extensions:
|
2020-09-12 17:43:17 +02:00
|
|
|
|
- pymdownx.highlight
|
|
|
|
|
- pymdownx.superfences
|
2020-07-17 16:50:57 +02:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
markdown_extensions:
|
|
|
|
|
- pymdownx.highlight:
|
|
|
|
|
use_pygments: false
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-21 13:33:44 +02:00
|
|
|
|
??? example "Syntax highlighting with Highlight.js"
|
2020-07-17 16:50:57 +02:00
|
|
|
|
|
2020-07-21 13:33:44 +02:00
|
|
|
|
[Highlight.js][8] can be integrated by creating an [additional
|
|
|
|
|
JavaScript][7] file initializing the highlighter and including the
|
|
|
|
|
respective stylesheet and JavaScript from a [CDN][9] serving
|
|
|
|
|
Highlight.js in `mkdocs.yml`:
|
2020-07-17 16:50:57 +02:00
|
|
|
|
|
2020-07-24 16:11:42 +02:00
|
|
|
|
=== "docs/javascripts/config.js"
|
2020-07-17 16:50:57 +02:00
|
|
|
|
|
2020-07-19 22:23:26 +02:00
|
|
|
|
``` js
|
|
|
|
|
hljs.initHighlighting()
|
|
|
|
|
```
|
2020-07-17 16:50:57 +02:00
|
|
|
|
|
2020-07-19 22:23:26 +02:00
|
|
|
|
=== "mkdocs.yml"
|
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
extra_javascript:
|
|
|
|
|
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js
|
2020-07-24 16:11:42 +02:00
|
|
|
|
- javascripts/config.js
|
2020-07-19 22:23:26 +02:00
|
|
|
|
extra_css:
|
|
|
|
|
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/default.min.css
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Note that Highlight.js has no affiliation with the Highlight extension.
|
|
|
|
|
|
2020-07-22 14:05:07 +02:00
|
|
|
|
`linenums`{: #linenums }
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2020-07-21 16:01:22 +02:00
|
|
|
|
: :octicons-milestone-24: Default: `false` – This option will add line numbers
|
2020-07-20 15:18:09 +02:00
|
|
|
|
to _all_ code blocks. If you wish to add line numbers to _some_, but not all
|
2020-07-21 13:33:44 +02:00
|
|
|
|
code blocks, consult the section on [adding line numbers][10] later in this
|
2020-07-20 15:18:09 +02:00
|
|
|
|
document, which also contains some tips on working with line numbers:
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
markdown_extensions:
|
|
|
|
|
- pymdownx.highlight:
|
|
|
|
|
linenums: true
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-23 17:00:20 +02:00
|
|
|
|
`linenums_style`{: #linenums-style }
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2020-07-21 16:01:22 +02:00
|
|
|
|
: :octicons-milestone-24: Default: `table` – The Highlight extension provides
|
2020-07-20 15:18:09 +02:00
|
|
|
|
three ways to add line numbers, all of which are supported by Material for
|
|
|
|
|
MkDocs. While `table` wraps a code block in a table, `inline` and
|
|
|
|
|
`pymdownx.inline` render line numbers as part of the line itself:
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
markdown_extensions:
|
|
|
|
|
- pymdownx.highlight:
|
|
|
|
|
linenums_style: pymdownx.inline
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Note that `inline` will put line numbers next to the actual code, which
|
|
|
|
|
means that they will be included when selecting text with the cursor or
|
|
|
|
|
copying a code block to the clipboard. Thus, the usage of `table` or
|
|
|
|
|
`pymdownx.inline` is recommended.
|
|
|
|
|
|
|
|
|
|
_Material for MkDocs doesn't provide official support for the other options of
|
|
|
|
|
this extension, so they may be supported but can also yield weird results. Use
|
|
|
|
|
them at your own risk._
|
2020-07-17 16:50:57 +02:00
|
|
|
|
|
2020-07-23 09:29:23 +02:00
|
|
|
|
[2]: https://github.com/squidfunk/mkdocs-material/blob/master/src/assets/stylesheets/main/extensions/pymdownx/_highlight.scss
|
2020-07-17 16:50:57 +02:00
|
|
|
|
[3]: https://facelessuser.github.io/pymdown-extensions/extensions/highlight/
|
2020-07-21 13:33:44 +02:00
|
|
|
|
[4]: https://python-markdown.github.io/extensions/code_hilite/
|
|
|
|
|
[5]: https://facelessuser.github.io/pymdown-extensions/
|
2020-09-19 14:34:40 +02:00
|
|
|
|
[6]: ../customization.md#additional-css
|
2020-07-21 13:33:44 +02:00
|
|
|
|
[7]: ../customization.md#additional-javascript
|
|
|
|
|
[8]: https://highlightjs.org/
|
|
|
|
|
[9]: https://cdnjs.com/libraries/highlight.js/
|
|
|
|
|
[10]: #adding-line-numbers
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
|
|
|
|
### InlineHilite
|
|
|
|
|
|
2020-07-21 13:33:44 +02:00
|
|
|
|
[:octicons-file-code-24: Source][2] · [:octicons-workflow-24: Extension][11]
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2020-07-21 13:33:44 +02:00
|
|
|
|
The [InlineHilite][11] extension, which is part of [Python Markdown
|
|
|
|
|
Extensions][5] also integrates with Material for MkDocs and adds support for
|
|
|
|
|
__syntax highlighting of inline code blocks__. It's built on top of the
|
2020-07-23 13:17:50 +02:00
|
|
|
|
[Highlight][3] extension and can be enabled via `mkdocs.yml`:
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
markdown_extensions:
|
|
|
|
|
- pymdownx.inlinehilite
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-21 13:33:44 +02:00
|
|
|
|
See the section on [inline code blocks][12] for usage information.
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2020-07-21 13:33:44 +02:00
|
|
|
|
[11]: https://facelessuser.github.io/pymdown-extensions/extensions/inlinehilite/
|
2020-07-22 11:57:41 +02:00
|
|
|
|
[12]: #highlighting-inline-code-blocks
|
2020-07-21 13:33:44 +02:00
|
|
|
|
|
2020-07-23 15:34:43 +02:00
|
|
|
|
### Keys
|
2020-07-21 13:33:44 +02:00
|
|
|
|
|
|
|
|
|
[:octicons-file-code-24: Source][13] · [:octicons-workflow-24: Extension][14]
|
|
|
|
|
|
2020-07-23 15:34:43 +02:00
|
|
|
|
The [Keys][14] extension, which is part of [Python Markdown Extensions][5],
|
|
|
|
|
allows for inserting __keyboard keys__, e.g. ++ctrl+alt+delete++ , and
|
|
|
|
|
can be enabled via `mkdocs.yml`:
|
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
markdown_extensions:
|
|
|
|
|
- pymdownx.keys
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
[13]: https://github.com/squidfunk/mkdocs-material/blob/master/src/assets/stylesheets/main/extensions/pymdownx/_keys.scss
|
|
|
|
|
[14]: https://facelessuser.github.io/pymdown-extensions/extensions/keys/
|
|
|
|
|
|
|
|
|
|
### SuperFences
|
|
|
|
|
|
|
|
|
|
The [SuperFences][15] extension, which is also part of [Python Markdown
|
2020-07-21 13:33:44 +02:00
|
|
|
|
Extensions][5], allows for the __nesting of code blocks inside other blocks__,
|
|
|
|
|
and is therefore strongly recommended:
|
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
markdown_extensions:
|
|
|
|
|
- pymdownx.superfences
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-23 15:34:43 +02:00
|
|
|
|
[15]: https://facelessuser.github.io/pymdown-extensions/extensions/superfences/
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2020-07-23 15:34:43 +02:00
|
|
|
|
### Snippets
|
2020-07-22 11:57:41 +02:00
|
|
|
|
|
2020-07-23 15:34:43 +02:00
|
|
|
|
The [Snippets][16] extension, which is also part of [Python Markdown
|
|
|
|
|
Extensions][5], allows to __insert content from other files__ or other, regular
|
|
|
|
|
content, and can be enabled via `mkdocs.yml`:
|
2020-07-22 11:57:41 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
markdown_extensions:
|
2020-07-23 15:34:43 +02:00
|
|
|
|
- pymdownx.snippets
|
2020-07-22 11:57:41 +02:00
|
|
|
|
```
|
|
|
|
|
|
2020-07-23 15:34:43 +02:00
|
|
|
|
[16]: https://facelessuser.github.io/pymdown-extensions/extensions/snippets/
|
2020-07-22 11:57:41 +02:00
|
|
|
|
|
2020-07-19 22:23:26 +02:00
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
This section discusses how to use different syntax highlighting features with
|
|
|
|
|
[Pygments][1] – the default highlighter – so they don't apply when using
|
2020-07-20 17:40:48 +02:00
|
|
|
|
a JavaScript syntax highlighter.
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
|
|
|
|
### Specifying the language
|
|
|
|
|
|
|
|
|
|
Code blocks must be enclosed with two separate lines containing three backticks.
|
|
|
|
|
To add code highlighting to those blocks, add the language short name directly
|
2020-07-22 11:57:41 +02:00
|
|
|
|
after the opening block. See the [list of available lexers][17] to find the
|
2020-07-19 22:23:26 +02:00
|
|
|
|
short name for a given language.
|
|
|
|
|
|
|
|
|
|
_Example_:
|
|
|
|
|
|
|
|
|
|
```` markdown
|
|
|
|
|
``` python
|
|
|
|
|
import tensorflow as tf
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
_Result_:
|
|
|
|
|
|
|
|
|
|
``` python
|
|
|
|
|
import tensorflow as tf
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-22 11:57:41 +02:00
|
|
|
|
[17]: https://pygments.org/docs/lexers/
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2021-02-28 14:23:11 +01:00
|
|
|
|
### Adding annotations
|
|
|
|
|
|
|
|
|
|
[:octicons-file-code-24: Source][18] ·
|
|
|
|
|
:octicons-beaker-24: Experimental ·
|
|
|
|
|
[:octicons-heart-fill-24:{: .mdx-heart } Insiders only][18]{: .mdx-insiders }
|
|
|
|
|
|
|
|
|
|
Annotations offer a comfortable and friendly way to attach explanations to
|
|
|
|
|
arbitrary sections of code blocks by adding simple markers within block/inline
|
|
|
|
|
comments that refer to items of a list following the code block, i.e. `(1)`,
|
|
|
|
|
`(2)`, etc. Material for MkDocs removes the list from the flow of the document,
|
|
|
|
|
injects the content of each list item into a tooltip, and links each list marker
|
|
|
|
|
to the corresponding tooltip.
|
|
|
|
|
|
|
|
|
|
In order to opt-in to annotation support, a slightly different syntax is
|
|
|
|
|
required – just add the respective [language short code][17] and the `.annotate`
|
|
|
|
|
class, after the three backticks.
|
|
|
|
|
|
|
|
|
|
_Example_:
|
|
|
|
|
|
|
|
|
|
```` markdown
|
|
|
|
|
``` {: .js .annotate }
|
|
|
|
|
document$.subscribe(function() { // (1)
|
|
|
|
|
var tables = document.querySelectorAll(/* (2) */ "article table")
|
|
|
|
|
tables.forEach(function(table) {
|
|
|
|
|
new Tablesort(table)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
1. ...
|
|
|
|
|
2. ...
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
_Result_:
|
|
|
|
|
|
|
|
|
|
<figure markdown="1">
|
|
|
|
|
|
|
|
|
|
[![Annotations][19]][19]
|
|
|
|
|
|
|
|
|
|
<figcaption markdown="1">
|
|
|
|
|
|
|
|
|
|
A demo is worth a thousand words — check it out at
|
|
|
|
|
[squidfunk.github.io/mkdocs-material-insiders][20]
|
|
|
|
|
|
|
|
|
|
</figcaption>
|
|
|
|
|
</figure>
|
|
|
|
|
|
|
|
|
|
_Annotations require syntax highlighting with [Pygments][24] – they're currently
|
|
|
|
|
not compatible with other JavaScript-based syntax highlighters. Support may be
|
|
|
|
|
added later on._
|
|
|
|
|
|
|
|
|
|
[18]: ../insiders.md
|
|
|
|
|
[19]: ../assets/screenshots/annotations.png
|
|
|
|
|
[20]: https://squidfunk.github.io/mkdocs-material-insiders/reference/code-blocks/#adding-annotations
|
|
|
|
|
|
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>"`
|
|
|
|
|
option directly after the short name, whereas `<start>` represents the starting
|
|
|
|
|
line number. A code block can start from a line number other than `1`, which
|
|
|
|
|
allows splitting large code blocks for readability.
|
|
|
|
|
|
|
|
|
|
_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`
|
|
|
|
|
argument placed right after the language short name. Note that line counts start
|
|
|
|
|
at `1`, regardless of the starting line number specified as part of `linenums`.
|
|
|
|
|
|
|
|
|
|
_Example_:
|
|
|
|
|
|
|
|
|
|
```` markdown
|
2020-07-20 17:40:48 +02:00
|
|
|
|
``` python hl_lines="2 3"
|
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]
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
_Result_:
|
|
|
|
|
|
2020-07-20 19:28:13 +02:00
|
|
|
|
``` python hl_lines="2 3"
|
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]
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-22 11:57:41 +02:00
|
|
|
|
### Highlighting inline code blocks
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
2021-02-28 14:23:11 +01:00
|
|
|
|
When [InlineHilite][21] is enabled, inline code blocks can be highlighted by
|
2020-07-19 22:23:26 +02:00
|
|
|
|
prefixing them with a shebang-like sequence, i.e. `#!`, directly followed by
|
2020-07-22 11:57:41 +02:00
|
|
|
|
the [language short name][17].
|
2020-07-19 22:23:26 +02:00
|
|
|
|
|
|
|
|
|
_Example_:
|
|
|
|
|
|
2020-07-24 09:59:16 +02:00
|
|
|
|
``` markdown
|
2020-07-19 22:23:26 +02: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.
|
|
|
|
|
|
2021-02-28 14:23:11 +01:00
|
|
|
|
[21]: #inlinehilite
|
2020-07-22 11:57:41 +02:00
|
|
|
|
|
2020-07-23 15:34:43 +02:00
|
|
|
|
### Adding keyboard keys
|
2020-07-22 11:57:41 +02:00
|
|
|
|
|
2021-02-28 14:23:11 +01:00
|
|
|
|
When [Keys][22] is enabled, keyboard keys can be rendered with a simple syntax.
|
2020-07-23 15:34:43 +02:00
|
|
|
|
Consult the [Python Markdown Extensions][16] documentation to learn about all
|
2020-07-26 14:46:09 +02:00
|
|
|
|
available key codes.
|
2020-07-22 11:57:41 +02:00
|
|
|
|
|
|
|
|
|
_Example_:
|
|
|
|
|
|
2020-07-24 09:59:16 +02:00
|
|
|
|
``` markdown
|
2020-07-22 11:57:41 +02:00
|
|
|
|
++ctrl+alt+del++
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
_Result_:
|
|
|
|
|
|
|
|
|
|
++ctrl+alt+del++
|
|
|
|
|
|
2021-02-28 14:23:11 +01:00
|
|
|
|
[22]: #keys
|
2020-07-23 15:34:43 +02:00
|
|
|
|
|
2020-07-23 17:00:20 +02:00
|
|
|
|
### Embedding external files
|
2020-07-23 15:34:43 +02:00
|
|
|
|
|
2021-02-28 14:23:11 +01:00
|
|
|
|
_Also known as transcludes or file transclusion in [MultiMarkdown][23]_.
|
2020-08-05 11:24:11 +02:00
|
|
|
|
|
2021-02-28 14:23:11 +01:00
|
|
|
|
When [Snippets][24] is enabled, content from other files can be embedded, which
|
2020-07-23 15:34:43 +02:00
|
|
|
|
is especially useful to reference and embed the contents of source files
|
|
|
|
|
directly into your project documentation.
|
|
|
|
|
|
|
|
|
|
_Example_:
|
|
|
|
|
|
|
|
|
|
```` markdown
|
|
|
|
|
```
|
|
|
|
|
--8<-- ".browserslistrc"
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
_Result_:
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-12 17:14:41 +01:00
|
|
|
|
last 4 years
|
2020-07-23 15:34:43 +02:00
|
|
|
|
```
|
|
|
|
|
|
2021-02-28 14:23:11 +01:00
|
|
|
|
Note that [Snippets][24] is not limited to code blocks, but can be used anywhere
|
2020-07-23 17:00:20 +02:00
|
|
|
|
from a document to move repeating content to separate files, which is also
|
2020-07-23 15:34:43 +02:00
|
|
|
|
explained in the [official documentation][16].
|
|
|
|
|
|
2021-02-28 14:23:11 +01:00
|
|
|
|
[23]: https://fletcher.github.io/MultiMarkdown-5/transclusion.html
|
|
|
|
|
[24]: #snippets
|
2020-07-23 17:00:20 +02:00
|
|
|
|
|
|
|
|
|
## Customization
|
|
|
|
|
|
|
|
|
|
### Custom syntax theme
|
|
|
|
|
|
2021-02-28 14:23:11 +01:00
|
|
|
|
[:octicons-file-code-24: Source][25] ·
|
2020-07-23 17:00:20 +02:00
|
|
|
|
:octicons-mortar-board-24: Difficulty: _easy_
|
|
|
|
|
|
2021-02-28 14:23:11 +01:00
|
|
|
|
If [Pygments][26] is used, Material for MkDocs provides the [styles for code
|
|
|
|
|
blocks][25], which are built with a custom and well-balanced palette that works
|
|
|
|
|
equally well for both [color schemes][27]:
|
2020-07-24 14:30:03 +02:00
|
|
|
|
|
2020-11-15 22:25:11 +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:
|
|
|
|
|
|
2020-11-15 22:25:11 +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-02-28 14:23:11 +01:00
|
|
|
|
several [types of string tokens][28], Material for MkDocs assigns a single color
|
2020-07-24 14:30:03 +02:00
|
|
|
|
to most of them.
|
|
|
|
|
|
2020-07-27 14:44:47 +02:00
|
|
|
|
Create an [additional stylesheet][6], and add:
|
2020-07-24 14:30:03 +02:00
|
|
|
|
|
|
|
|
|
``` css
|
2020-07-29 16:47:22 +02:00
|
|
|
|
:root > * {
|
2020-07-24 14:30:03 +02:00
|
|
|
|
--md-code-hl-string-color: #0FF1CE;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
If you want to tweak a specific type of string, i.e. ``#!js `backticks` ``, you
|
2021-02-28 14:23:11 +01:00
|
|
|
|
can lookup the specific class name in the [syntax theme definition][29], and
|
2020-07-24 14:30:03 +02:00
|
|
|
|
override it as part of your additional stylesheet:
|
|
|
|
|
|
|
|
|
|
``` css
|
|
|
|
|
.highlight .sb {
|
|
|
|
|
color: #0FF1CE;
|
|
|
|
|
}
|
|
|
|
|
```
|
2020-07-23 17:00:20 +02:00
|
|
|
|
|
2021-02-28 14:23:11 +01:00
|
|
|
|
[25]: https://github.com/squidfunk/mkdocs-material/blob/master/src/assets/stylesheets/main/_colors.scss#
|
|
|
|
|
[26]: #use-pygments
|
|
|
|
|
[27]: ../setup/changing-the-colors.md#color-scheme
|
|
|
|
|
[28]: https://pygments.org/docs/tokens/#literals
|
|
|
|
|
[29]: https://github.com/squidfunk/mkdocs-material/blob/master/src/assets/stylesheets/main/extensions/markdown/_codehilite.scss
|