# Code blocks

> Show code with syntax highlighting, a filename in the header, and a copy button. Group variants into tabbed code groups.

Code blocks are standard Markdown fences. Typemark highlights them with Shiki and adds a header bar showing the language or filename.

```javascript
const response = await fetch("https://api.acme.com/users", {
  headers: { Authorization: `Bearer ${apiKey}` },
});
const users = await response.json();
```

````mdx
```javascript
const response = await fetch("https://api.acme.com/users", {
  headers: { Authorization: `Bearer ${apiKey}` },
});
const users = await response.json();
```
````

Code blocks are dark in both light and dark mode.

## Languages

Put the language right after the opening backticks. Shiki covers the languages you'd expect, for example:

| Area | Languages |
|------|-----------|
| Web | `javascript`, `typescript`, `jsx`, `tsx`, `html`, `css`, `scss`, `json`, `graphql` |
| Backend | `python`, `ruby`, `go`, `java`, `rust`, `php`, `c`, `cpp`, `csharp`, `kotlin`, `swift` |
| Ops and data | `bash`, `powershell`, `dockerfile`, `yaml`, `toml`, `sql`, `diff` |
| Writing | `markdown`, `mdx` |

Short aliases such as `js`, `ts`, and `sh` work too. A fence with no language, or one Shiki doesn't recognise, renders as plain text with no header bar.

## Filenames and titles

Write a filename after the language and it replaces the language in the header.

```python app.py
from flask import Flask

app = Flask(__name__)
```

````mdx
```python app.py
from flask import Flask

app = Flask(__name__)
```
````

You can also use `title="…"`. The header shows it in a monospace font, and it wins over a plain label if a fence has both.

```yaml title="config/database.yml"
production:
  adapter: postgresql
  pool: 5
```

````mdx
```yaml title="config/database.yml"
production:
  adapter: postgresql
  pool: 5
```
````

| Fence option | Example | What it does |
|--------------|---------|--------------|
| Language | ` ```ruby ` | Sets highlighting. Shown in the header when there's no label or title. |
| Label | ` ```ruby Gemfile ` | Any plain words after the language. Shown in the header and used as the tab name in a code group. |
| `title` | ` ```ruby title="Gemfile" ` | Shown in the header. Used as a code group tab name only when there's no label. |

Words like `wrap`, `lines`, or a range like `{1-3}` after the language are ignored, so they never end up in the label.

## Code groups

A code group puts several code blocks behind tabs in a single frame. Use it for the same command across package managers, or the same call in different languages.

<CodeGroup>
  ```bash npm
  npm install @acme/sdk
  ```
  ```bash yarn
  yarn add @acme/sdk
  ```
  ```bash pnpm
  pnpm add @acme/sdk
  ```
</CodeGroup>

````mdx
<CodeGroup>
  ```bash npm
  npm install @acme/sdk
  ```
  ```bash yarn
  yarn add @acme/sdk
  ```
  ```bash pnpm
  pnpm add @acme/sdk
  ```
</CodeGroup>
````

Each tab is named after its block's label, then its `title`, then its language. Give every block a label so the tabs read clearly.

<CodeGroup>
  ```javascript Node.js
  const acme = require("@acme/sdk");
  const client = new acme.Client({ apiKey: process.env.ACME_KEY });
  ```
  ```python Python
  import os, acme
  client = acme.Client(api_key=os.environ["ACME_KEY"])
  ```
  ```ruby Ruby
  require "acme"
  client = Acme::Client.new(api_key: ENV["ACME_KEY"])
  ```
</CodeGroup>

````mdx
<CodeGroup>
  ```javascript Node.js
  const acme = require("@acme/sdk");
  const client = new acme.Client({ apiKey: process.env.ACME_KEY });
  ```
  ```python Python
  import os, acme
  client = acme.Client(api_key=os.environ["ACME_KEY"])
  ```
  ```ruby Ruby
  require "acme"
  client = Acme::Client.new(api_key: ENV["ACME_KEY"])
  ```
</CodeGroup>
````

`<CodeGroup>` takes no props. Put only fenced code blocks inside it. For tabs that mix prose and code, use [Tabs](https://docs.typemark.dev/components/tabs).

## Copy button

Every code block gets a **Copy** button when `copy_code_button` is on in `typemark.json`. New projects start with it on. A code group has one button that copies the tab that's showing.

```json
{
  "features": {
    "copy_code_button": true
  }
}
```

You can also switch it in your project settings under **Copy code button**.

## Diagrams

A fence with the language `mermaid` draws a diagram instead of showing code. See [Diagrams](https://docs.typemark.dev/components/diagrams).

## Inline code

Wrap text in single backticks for inline code: `npm run build`. In the visual editor, select text and press `Cmd+E`.

```mdx
Run `npm run build` before you deploy.
```

## In the visual editor

Type `/` and pick **Code block** or **Code group**. Each block has a language dropdown in its top-right corner. **Plain text** removes the language.

In a code group, the tabs work like [Tabs](https://docs.typemark.dev/components/tabs): add, rename, reorder, or remove them. Renaming a tab writes the new label into that block's fence, so ` ```bash npm ` stays readable in source mode too.
