Skip to content

Code Blocks

You're probably farmilliar with the basic code blocks most mark down applications provide. There are a few handy features available that can make your code snippets much easier to read.

Usage

Syntax Highlighting

Write the language shortcode after the opening ``` for code blocks:

```cpp
#include <iostream>

constexpr GREETING = "Hello, world!"

int main()
{
    std::cout << GREETING << std::endl;

    return 0;
}
```
#include <iostream>

constexpr GREETING = "Hello, world!"

int main()
{
    std::cout << GREETING << std::endl;

    return 0;
}

Inline snippets

For inline code snippets use the syntax `#!py console.log("Hello, world")` where py is the language shortcode.

Titles

Add title="<custom title>" to the opening line after the language name replacing <custom title> with the actual title:

```cpp title="main.cpp"
#include <iostream>

constexpr GREETING = "Hello, world!"

int main()
{
    std::cout << GREETING << std::endl;

    return 0;
}
```
main.cpp
#include <iostream>

constexpr GREETING = "Hello, world!"

int main()
{
    std::cout << GREETING << std::endl;

    return 0;
}

Line numbers

Line numbers can be added by using linenums="<start>" after the language name. The argument <start> is the number to start with on the first line.

```cpp linenums="5"
int main()
{
    std::cout << GREETING << std::endl;

    return 0;
}
```
int main()
{
    std::cout << GREETING << std::endl;

    return 0;
}

Highlighted Lines

Add hl_lines="<lines>" after the language name to enable this feature. The <lines> argument can be a list of individual numbers seperated with spaces or a range seperated by a dash or both. The line numbers used are independent of the linenums argument.

```cpp hl_lines="1 3 5-7"
#include <iostream>

constexpr GREETING = "Hello, world!"

int main()
{
    std::cout << GREETING << std::endl;

    return 0;
}
```
#include <iostream>

constexpr GREETING = "Hello, world!"

int main()
{
    std::cout << GREETING << std::endl;

    return 0;
}

Annotations

Annotations rely on comments from syntax highlighting and work similarly to text annotation. Place the annotation marker in a comment of the specified language then (optionally use a ! to hide the extra comment characters) add your content in an ordered list after the code block.

```cpp title="main.cpp"
#include <iostream>

constexpr GREETING = "Hello, world!"

int main()
{
    std::cout << GREETING << std::endl; // (1)!

    return 0;
}
```

1. You should really be using `#!cpp std::println` instead of `#!cpp std::cout`!
main.cpp
#include <iostream>

constexpr GREETING = "Hello, world!"

int main()
{
    std::cout << GREETING << std::endl; // (1)!

    return 0;
}
  1. You should really be using std::println instead of std::cout!