Codeblocks
Codeblocks¶
Adding annotations¶
codeblock languages¶
In order to have codeblocks show syntax highlights note the language like:
Some of the popular ones are: - html - py - md - css - jsI'm a code annotation! I can contain
code
, formatted text, images, ... basically anything that can be written in Markdown.
Highlighting inline code blocks¶
small inline codeblocks can be used with laguage syntax too like:
Therange()
function is used to generate a sequence of numbers.
Adding a title¶
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 annotations¶
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 // ...
and
/* ... */
, for YAML in # ...
, etc.1:
``` yaml
theme:
features:
- content.code.annotate # (1)
```
1. :man_raising_hand: I'm a code annotation! I can contain `code`, __formatted
text__, images, ... basically anything that can be written in Markdown.
I'm a code annotation! I can contain
code
, formatted text, images, ... basically anything that can be written in Markdown.
Adding line numbers¶
Highlight specefic lines¶
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 titles¶
In order to provide additional context, a custom title can be added to a code block by using the title="
namefile.py | |
---|---|
Filename as title¶
In order to show a filename as title you set up the following: ```md
=== ":octicons-file-code-16: `cartographer.conf`"
``` linenums="1" hl_lines="4 5"
[update_manager Cartographer]
type: git_repo
path: usrdatacartographer-klipper
channel: stable
primary_branch: master
origin: https://github.com/Cartographer3D/cartographer-klipper.git
install_script: install.sh
is_system_service: False
managed_services: klipper
info_tags:
desc=Cartographer Probe
```
```
:octicons-file-code-16:
is the icon shown.
Content tabs¶
-
Code annotations require syntax highlighting with [Pygments] – they're 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. ↩