Where code can live
Markdown lets code appear in several places, and each needs slightly different handling to splice highlighted HTML back in without breaking the surrounding structure.
At the top level
The simple case: a fence at column 0.
fn main() {
let greeting = "hello";
println!("{greeting}, world");
}
Inside a list
Indented fenced blocks such as the ones inside a list also works:
-
First, define the function:
def area(radius: float) -> float: return 3.14159 * radius ** 2 -
Then call it:
print(area(2.0))-
Even nested two levels deep:
int main(void) { return 0; }
-
Inside a block quote
A quoted example:
#[derive(Debug)] struct Point { x: f64, y: f64 }Deeply nested block quotes also work
#[derive(Debug)] struct Point { x: f64, y: f64 }…and the quote continues afterwards.
Inline
Markdown has no syntax for tagging inline code blocks. We parse each
inline block to inspect a language tag before the first backtick (`):
```rust`let x = 1;``` # use triple-backtick to quote the backtick, tagging as rust
This gives: let x = 1;.
It works for any language, so lambda x: x + 1 and
static const int N = 8; are highlighted too. Ordinary inline code such as
--flag or book.toml is left exactly as it was.
Mixed into a table
| Language | Declaration |
|---|---|
| Rust | let x: u8 = 1; |
| C | uint8_t x = 1; |
| Python | x: int = 1 |