---
title: "Code Blocks"
description: "Code blocks are highlighted with github-light / github-dark and support filenames, line highlights, a copy button, and color and icon previews. Use code-group, code-tree, code-preview and code-collapse for multi-file, multi-package-manager and long examples."
canonical_url: "https://docs.mhaibaraai.cn/en/docs/writing/code-blocks"
---
# Code Blocks

> Code blocks are highlighted with github-light / github-dark and support filenames, line highlights, a copy button, and color and icon previews. Use code-group, code-tree, code-preview and code-collapse for multi-file, multi-package-manager and long examples.

## Basics

### Inline code

Wrap inline code in backticks. Add `{lang="ts-type"}` for TypeScript highlighting:

```mdc
`useMovkI18n()` returns `Ref<string>`{lang="ts-type"}
```

### Code blocks

Put the language after three backticks. Light mode uses `github-light` and dark mode `github-dark`, switching with the site theme:

````mdc
```ts
export default defineNuxtConfig({
  extends: ['@movk/nuxt-docs']
})
```
````

The layer preloads `bash`, `shell`, `diff`, `json`, `js`, `ts`, `html`, `css`, `vue`, `mdc`, `md` and `yaml`, plus `mermaid` when Mermaid is enabled. Add other languages to `content.build.markdown.highlight.langs`.

### Filenames

Put a filename in square brackets after the language. The header shows the filename with an icon matched by extension:

````mdc
```ts [nuxt.config.ts]
export default defineNuxtConfig({
  extends: ['@movk/nuxt-docs']
})
```
````

### Line highlights

List the lines to highlight in curly braces after the filename, as single lines or ranges:

````mdc
```ts [nuxt.config.ts] {3-5}
export default defineNuxtConfig({
  extends: ['@movk/nuxt-docs'],
  movkNuxtDocs: {
    mermaid: true
  }
})
```
````

### Copy button

Every code block has a copy button in the top-right corner. After a click its label changes from "Copy" to "Copied" and resets after about 2 seconds. The label follows the current language.

### Color and icon previews

Literal color values in code (such as `#d64b34` or `rgb(214 75 52)`) get a swatch, and icon names such as `i-lucide-*` get the matching icon. Tailwind palette names (such as `primary: 'vermilion'`) get no swatch:

```ts [app/app.config.ts]
export default defineAppConfig({
  ui: {
    colors: {
      primary: 'vermilion'
    }
  },
  toc: {
    bottom: {
      links: [{ icon: 'i-lucide-message-circle-code', label: 'llms.txt', to: '/llms.txt' }]
    }
  }
})
```

```css [app/assets/css/main.css]
@theme static {
  --color-brand-500: #d64b34;
}
```

> [!TIP]
> See: https://ui.nuxt.com/getting-started/icons/nuxt#theme
> 
> Code block icons can be customized in `app/app.config.ts`:
> 
> ```ts [app/app.config.ts]
> export default defineAppConfig({
>   ui: {
>     prose: {
>       codeIcon: {
>         terminal: 'i-lucide-square-terminal'
>       }
>     }
>   }
> })
> ```

## Combining code blocks

### `CodeGroup`

`code-group` turns several code blocks into tabs, for different package managers or alternative approaches:

````mdc
::code-group

```bash [pnpm]
pnpm add @movk/nuxt-docs
```

```bash [npm]
npm install @movk/nuxt-docs
```

```bash [yarn]
yarn add @movk/nuxt-docs
```

```bash [bun]
bun add @movk/nuxt-docs
```

::
````

Code groups whose tabs are all package managers (`pnpm`, `npm`, `yarn`, `bun`, `deno`) or all TypeScript / JavaScript code stay in sync across the site: switching to `npm` in one place switches every similar group on other pages too, and the choice is remembered on the next visit. The layer puts the tabs of these groups in a consistent order. To switch a group independently, give it its own `sync` value, for example `::code-group{sync="my-group"}`.

### `CodeTree`

`code-tree` shows a set of related files as a file tree; `default-value` selects the file opened first:

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  extends: ['@movk/nuxt-docs']
})
```

```ts [app/app.config.ts]
export default defineAppConfig({
  header: {
    title: 'My Docs'
  }
})
```

```md [content/index.md]
---
title: My Docs
---

# My Docs
```

```md [content/docs/1.getting-started/1.index.md]
---
title: Introduction
description: One sentence about the project.
---

## Quick start
```

```json [package.json]
{
  "name": "my-docs",
  "private": true,
  "type": "module",
  "scripts": {
    "dev": "nuxt dev",
    "build": "nuxt build",
    "generate": "nuxt generate"
  },
  "dependencies": {
    "@movk/nuxt-docs": "^3.0.0",
    "nuxt": "^4.0.0",
    "tailwindcss": "^4.0.0"
  }
}
```

### `CodePreview`

`code-preview` stacks the rendered result above its source: the default slot holds what to render and the `#code` slot holds the code to show. Every example on this page uses it:

````mdc
::code-preview
`inline code`

#code
```mdc
`inline code`
```
::
````

### `CodeCollapse`

`code-collapse` folds long code until the reader expands it:

````mdc
::code-collapse

```css [app/assets/css/colors.css]
@theme static {
--color-vermilion-50: #fdf3f1;
/* ... */
--color-vermilion-950: #3c110a;
}
```

::
````


## Sitemap

See the full [sitemap](https://docs.mhaibaraai.cn/sitemap.md) for all pages.
