Packages
mdex
0.11.3
0.13.3
0.13.2
0.13.1
0.13.0
0.12.5
retired
0.12.4
0.12.3
0.12.2
0.12.1
0.12.0
0.11.7
0.11.6
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.0
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.2
0.6.1
0.6.0
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.0
0.1.18
0.1.17
0.1.16
0.1.15
0.1.14
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Fast and extensible Markdown for Elixir
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
guides/code_block_decorators.md
Code block decorators allow you to customize the appearance and behavior of individual code blocks by adding special attributes to the info string (the part after the opening backticks).
### Prerequisites
To use code block decorators, you must enable both `:render` options:
```elixir
render: [
github_pre_lang: true,
full_info_string: true
]
```
### Available Decorators
| Decorator | Description | Supported Formatters | Example |
|-----------|-------------|---------------------|---------|
| `theme` | Override the syntax highlighting theme | All | `theme=github_dark` |
| `pre_class` | Add custom CSS classes to `<pre>` element | All | `pre_class="my-class"` |
| `highlight_lines` | Highlight single and/or range of lines | All | `highlight_lines="1,3-5"` |
| `highlight_lines_style` | Custom inline styles for highlighted lines | HTML inline only | `highlight_lines_style="background: yellow"` |
| `highlight_lines_class` | Custom CSS class for highlighted lines | All | `highlight_lines_class="emphasis"` |
| `include_highlights` | Add syntax token names as data attributes | All | `include_highlights` |
### Examples
_Following examples assume `render: [github_pre_lang: true, full_info_string: true]` is set._
You can find a Livebook at [Examples / Code Block Decorators](https://hexdocs.pm/mdex/code_block_decorators-1.html)
#### Override Theme
Change the syntax highlighting theme for a specific code block:
````md
```elixir theme=github_dark
def hello do
"Hello, world!"
end
```
````
Output: `<pre class="athl" style="color: #c9d1d9; background-color: #0d1117;">...`
#### Add Custom CSS Classes
Add your own CSS classes to the `<pre>` element:
````md
```javascript pre_class="code-example interactive"
console.log("Hello!");
```
````
Output: `<pre class="athl code-example interactive">...`
#### Highlight Specific Lines
Highlight individual lines or ranges (inclusive):
````md
# Highlight lines 1, 4, 5, and 6
```python highlight_lines="1,4-6"
import math
def calculate(x):
result = x * 2
# return calculated result
return math.sqrt(x)
```
````
With `:html_inline` formatter, lines get styles from the theme's highlight color, for eg:
```html
<span style="background-color: #dae9f9;" data-line="1">...
```
With `:html_linked` formatter, the class `highlighted` is added to the highlighted lines, for eg:
```html
<span class="line highlighted" data-line="1">...
```
#### Custom Highlight Styling
Use either `highlight_lines_style` or `highlight_lines_class` to customize the appearance of highlighted lines:
````md
```ruby highlight_lines="2" highlight_lines_style="background: #ffeb3b; font-weight: bold;"
class User
def initialize(name)
@name = name
end
end
```
````
#### Include Syntax Token Information
Add syntax token names in `data-highlight` attributes, useful for debugging or custom styling:
````md
```rust include_highlights
let x: i32 = 42;
```
````
Output: `<span data-highlight="keyword">let</span>`
#### Combine Multiple Decorators
Use multiple decorators together:
````md
```typescript theme=github_light pre_class="example" highlight_lines="2-3" include_highlights
interface User {
name: string; // highlighted
email: string; // highlighted
age?: number;
}
```
````
### Important Notes
1. **Formatter Support**: Not all decorators work with all formatters:
- `highlight_lines_style` only works with `:html_inline` formatter
- `theme` only works with `:html_inline` formatter
2. **CSS Classes**: The `athl` class is always added to `<pre>` elements when using syntax highlighting
3. **Line Numbers**: The `data-line` attribute is added to each line for reference
4. **Order**: It's expected the first word of the code fence info string to be the language name, followed by decorators.
- For example, `elixir theme=github_dark` is valid, but `theme=github_dark elixir` is not.
5. **Performance**: Decorators are processed at render time, so using many decorators may impact performance