Code Blocks¶
Material for MkDocs provides different ways to set up syntax highlighting for code blocks and comes with a copy code button.
Code Block Usage¶
Adding a tile¶
You can add a title to the code block by suffixing title="titleName" after the shortcode.
``` 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]
```
Adding Line Numbers¶
Line numbers can be added to a code block by by suffixing linenums="<start>" after the shortcode. <start> represents the starting line number. A code block can start from a line number other than 1.
``` py 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 shortcode. Note that line counts start at 1, regardless of the starting line number specified as part of linenums:
Highlighting inline code blocks¶
Syntax highlighting can be applied to inline code blocks by prefixing them with a #!, directly followed by the corresponding language shortcode.
The range() function is used to generate a sequence of numbers.