Source code and APIs

1. Basic block syntax highlighting

Code blocks contain a language identifier, which is used to determine the language of the code. This language is used to determine the syntax highlighting, using an available pygments lexer.

Markdown:

```python
from a import b
c = "string"
```

Rendered Output:

from a import b
c = "string"

Markdown:


::::{admonition} Show backticks inside raw markdown blocks :class: tip dropdown

If you’d like to show backticks inside of your markdown, you can do so by nesting them in backticks of a greater length. Markdown will treat the outer-most backticks as the edges of the “raw” block and everything inside will show up. For example: Markdown:

`` `hi` ``

````
```
hi
```

::::

Rendered Output:

`hi`

```
hi
```

Numbering and highlighting lines

To set a global default for line numbering, per lexer name, the myst_number_code_blocks configuration option can be used. For example, using:

myst_number_code_blocks = ["typescript"]

Will number all code blocks with the typescript lexer by default.

Markdown:

```typescript
type MyBool = true | false;

interface User {
  name: string;
  id: number;
}
```

Rendered Output:

type MyBool = true | false;

interface User {
  name: string;
  id: number;
}

To apply numbering and highlighting to a specific code block, the attrs_block extension can be used:

Markdown:

{lineno-start=1 emphasize-lines="2,3"}
```python
a = 1
b = 2
c = 3
```

Rendered Output: {lineno-start=1 emphasize-lines=”2,3”}

a = 1
b = 2
c = 3

The following options are recognized:

:::{admonition} Code block options :class: hint dropdown

linenos : flag : Enable to generate line numbers for the code block

lineno-start : integer : The starting line number for the code block (linenos is automatically enabled)

emphasize-lines : comma-separated list of integers : Highlight the specified lines

caption : string : The caption for the code block

force : flag : Allow minor errors on highlighting to be ignored

name : string : The name of the code block, which can be referenced elsewhere in the document

class : string : The class to apply to the code block

:::

reference3, reference4 –>

-->