Skip to Content
Vuesho

Code Blocks

Code highlighting, themes and code tabs.

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 blocks are highlighted with @pierre/highlights, in GitHub Light and GitHub Dark. To use other themes, install the package and import them from @pierre/highlights/themes:

npm i @pierre/highlights
vuesho.config.ts
import ayuLight from "@pierre/highlights/themes/ayu-light"; 
import dracula from "@pierre/highlights/themes/dracula"; 

createSite(docs.toSource()).config({
  markdown: { highlight: { themes: { light: ayuLight, dark: dracula } } }, 
});

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().

Transformers

markdown.highlight.transformers takes Shiki transformers, which change the HTML of each code block. For their type, install @shikijs/types:

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:

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] } }, 
});

When a transformer adds an element with a capitalized tag, like Badge, the page renders that component.

Other Highlighters

markdown.highlight.highlighter replaces the highlighter. createShikiHighlighter() from vuesho/highlight/shiki creates a highlighter that uses Shiki:

npm i shiki
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().

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.

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

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:

```package-install
vuesho vue
```
npm install vuesho vue

To change the tabs, pass npm options to the recommended preset. 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:

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" },
    },
  }),
);

Prop

Type

JavaScript Tabs

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

npm i -D @vuesho/typescript typescript@6
vuesho.config.ts
import { ts2jsPlugin } from "@vuesho/typescript"; 

createSite(docs.toSource()).plugins(
  recommendedPreset(),
  ts2jsPlugin(), 
);

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

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

The JavaScript keeps the comments and the 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:

Prop

Type