Skip to content

Source🔗

Setup🔗

theme:
  features:
    - content.code.annotate # (1)!
    - content.code.copy # (2)!

markdown_extensions:
  - pymdownx.highlight:
      anchor_linenums: true
      line_spans: __span
      pygments_lang_class: true
  - pymdownx.inlinehilite
  - pymdownx.snippets
  - pymdownx.superfences
  1. Important for code annotations
  2. Enable code copy button

Examples🔗

Code with highlighting:

def example(param):
    print(f'param = {param}')

With title:

Python example
def example(param):
    print(f'param = {param}')

With line numbers and line highlight:

1
2
3
def example(param):
    print(f'param = {param}')
    return param

Highlighting inline code is done with the #! characters, e.g., print(f'param = {param}').

Using snippets:

hello_world.c
#include<stdio.h>
#include<stdlib.h>

/*
 * Hello World
 */
int main(int argc, char *argv[]) {
  int a = 5;
  int b = 4.3;

  printf("Hello World!\n");

  return EXIT_SUCCESS;
}
``` c title="hello_world.c"
--8<-- "writing/mkdocs/hello_world.c"
```

The filename is under the directory base_path configured in mkdocs.yaml:

markdown_extensions:
  - pymdownx.snippets:
      base_path: snippets/

The directory should be outside of docs/.

More examples can be found in the documentation.

Using annotations:

// Simple example
int main(int argc, char* argv[])
{
  int a = 42; // The answer (1)
  return EXIT_SUCCESS; // (2)!
}
  1. to everything.
  2. Annotation with stripping comments
``` c
// Simple example
int main(int argc, char *argv[])
{
  int a = 42;          // The answer (1)
  return EXIT_SUCCESS; // (2)!
```

1.  to everthing
2.  Annotation with stripping comments

For each code annotation a link can be set (example).

Available Languages/Lexer🔗

See: https://pygments.org/docs/lexers/

bash / sh🔗

#!/usr/bin/env bash

if [ $? != 0 ]
then
  echo "${USER:-}"
else
  echo "${HOSTNAME:-}"
fi

console / shell-session🔗

$ whoami
dreknix

python🔗

def example(param):
    print(f'param = {param}')

pycon🔗

>>> name = 'dreknix'
>>> print(name)
dreknix
>>> div = 1 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

pwsh / powershell🔗

$content = [IO.File]::ReadAllText('c:\samplefile.txt')
Get-Item -Path C:\Temp

pwsh-session🔗

PS> Get-Item -Path C:\Temp
dreknix
PS C:\> $content = [IO.File]::ReadAllText('c:\samplefile.txt')

batch🔗

REM off
ECHO ms-dos

doscon🔗

C:\> ECHO ms-dos
ms-dos

text🔗

Formatted text without highlighting

todotxt🔗

todotxt/todo.txt

(A) 2024-01-01 example todo +writing @mkdocs due:2024-01-31
(B) 2024-01-01 example todo +writing @mkdocs due:2024-01-31 label:free
x (A) 2024-02-01 2024-01-01 example todo +writing @mkdocs due:2024-01-31