# Code Blocks

Code highlighting, themes and code tabs.

[Code blocks](/markdown#code-blocks) are highlighted when the site builds. This page covers their themes, other highlighters, and the tabs that a code block can become.

## Code Themes [#code-themes]

Code blocks are highlighted with [`@pierre/highlights`](https://www.npmjs.com/package/@pierre/highlights), in GitHub Light and GitHub Dark. To use other themes, install the package and import them from `@pierre/highlights/themes`:

```npm
npm i @pierre/highlights
```

```ts title="vuesho.config.ts"
import ayuLight from "@pierre/highlights/themes/ayu-light"; // [!code ++]
import dracula from "@pierre/highlights/themes/dracula"; // [!code ++]

createSite(docs.toSource()).config({
  markdown: { highlight: { themes: { light: ayuLight, dark: dracula } } }, // [!code ++]
});
```

A language the highlighter doesn't support renders as plain text. The same themes apply to code that the UI highlights in the browser, and to code that a plugin highlights with [`highlightCode()`](/content-hooks#page-bodies).

## Transformers [#transformers]

`markdown.highlight.transformers` takes [Shiki transformers](https://shiki.style/guide/transformers), which change the HTML of each code block. For their type, install `@shikijs/types`:

```npm
npm i -D @shikijs/types @types/hast
```

This transformer shows a terminal icon on shell code blocks, and removes the icon from the others. `enforce: "post"` runs it after the built-in transformers, which add the language icon:

```ts title="vuesho.config.ts"
import type { ShikiTransformer } from "@shikijs/types";

const icons: ShikiTransformer = {
  name: "icons",
  enforce: "post",
  pre(pre) {
    if (this.options.lang === "sh") pre.properties.icon = `<svg viewBox="0 0 24 24">…</svg>`;
    else delete pre.properties.icon;
  },
};

createSite(docs.toSource()).config({
  markdown: { highlight: { transformers: [icons] } }, // [!code highlight]
});
```

When a transformer adds an element with a capitalized tag, like `Badge`, the page renders that [component](/markdown/components#custom-components).

## Other Highlighters [#other-highlighters]

`markdown.highlight.highlighter` replaces the highlighter. `createShikiHighlighter()` from `vuesho/highlight/shiki` creates a highlighter that uses [Shiki](https://shiki.style):

```npm
npm i shiki
```

```ts title="vuesho.config.ts"
import { createHighlighter } from "shiki";
import { createShikiHighlighter } from "vuesho/highlight/shiki";

createSite(docs.toSource()).config({
  markdown: {
    highlight: {
      highlighter: ({ transformers }) =>
        createShikiHighlighter(() => createHighlighter({ themes: [], langs: [] }), {
          transformers,
        }),
    },
  },
});
```

Shiki highlights in GitHub Light and GitHub Dark, unless you pass `themes` in the second argument of `createShikiHighlighter()`.

<Callout title="Transformers">
The `highlighter` function receives the site's transformers: the line notations, the icons and `markdown.highlight.transformers`. Pass them to the highlighter, or none of them apply.
</Callout>

A custom highlighter implements `Highlighter` from `vuesho/highlight`. Code that the UI highlights in the browser keeps the built-in highlighter.

## Package Managers [#package-managers]

An `npm` code block shows its command as a tab for npm, pnpm, Yarn and Bun. A `package-install` block takes only the package names:

````md
```package-install
vuesho vue
```
````

```package-install
vuesho vue
```

To change the tabs, pass `npm` options to the [recommended preset](/config#recommendedpreset). Each package manager's `command` converts the npm command to that manager's command, or returns nothing to skip the block. `persist` keeps the reader's choice across pages and visits:

```ts title="vuesho.config.ts"
createSite(docs.toSource()).plugins(
  recommendedPreset({
    npm: {
      packageManagers: [
        { name: "npm", command: (code) => code },
        { name: "pnpm", command: (code) => code.replace(/^npm (i|install) /gm, "pnpm add ") },
      ],
      persist: { id: "package-manager" },
    },
  }),
);
```

### NpmOptions

| Prop               | Type                                | Description                                                                                      |
| ------------------ | ----------------------------------- | ------------------------------------------------------------------------------------------------ |
| `packageManagers?` | `PackageManager[] \| undefined`     | Default: `npm, pnpm, Yarn and Bun`                                                               |
| `persist?`         | `false \| TabsPersist \| undefined` | Shares the package manager a reader picks across blocks, unless a block has its own `tab-group`. |

## JavaScript Tabs [#javascript-tabs]

With `ts2jsPlugin()` from `@vuesho/typescript`, a TypeScript code block can also show its JavaScript. Install the package and add the plugin:

```npm
npm i -D @vuesho/typescript typescript@6
```

```ts title="vuesho.config.ts"
import { ts2jsPlugin } from "@vuesho/typescript"; // [!code ++]

createSite(docs.toSource()).plugins(
  recommendedPreset(),
  ts2jsPlugin(), // [!code ++]
);
```

Then add `ts2js` after `ts` or `tsx`, and the block becomes two tabs:

````md
```ts ts2js title="add.ts"
// adds two numbers
export const add = (a: number, b: number): number => a + b; // [\!code highlight]
```
````

```ts ts2js title="add.ts"
// adds two numbers
export const add = (a: number, b: number): number => a + b; // [!code highlight]
```

The JavaScript keeps the comments and the [notations](/markdown#line-notations), without the types. A `tsx` block becomes `jsx`. A syntax error fails the build at its line.

Both tabs take the block's other attributes, like `title`. To give the JavaScript tab another title, write `ts2js='title="add.js"'`. The options below can turn every TypeScript block into tabs, and `tabGroup` keeps the reader's language across blocks:

### Ts2jsPluginOptions

| Prop              | Type                        | Description                                                                                            |
| ----------------- | --------------------------- | ------------------------------------------------------------------------------------------------------ |
| `disableTrigger?` | `boolean \| undefined`      | Converts every `ts` and `tsx` code block, even without `ts2js` in its meta. Default: `false`           |
| `defaultValue?`   | `"ts" \| "js" \| undefined` | The tab open first. Defaults to the first tab. Default: `"ts"`                                         |
| `tabGroup?`       | `string \| undefined`       | The tabs' `tab-group`, so the language a reader picks holds across the blocks and pages that share it. |
