Syntax highlighting

View source

Configure Shiki themes, custom theme objects, the language allowlist, and custom languages.

typescript
export default {
  shiki: {
    themes: {
      light: "github-light",
      dark: "github-dark",
    },
    langs: ["typescript", "tsx", "javascript", "jsx", "json", "css"],
  },
};

Only languages listed in shiki.langs are enabled for highlighting. They are loaded on demand, while unsupported or unlisted languages safely render as plain text. Keep the list limited to formats used by the site so production bundles stay small.

Highlighting in the browser

Fenced code blocks are highlighted once, on the server, while Folio compiles the page. The browser renders that HTML as it is and does not highlight those blocks again.

A CodeBlock used directly in MDX or in your own components, for example <CodeBlock lang="toml" code={source} />, has no server-highlighted children, so it is highlighted in the browser. It uses the same language set as the server: the built-in languages that shiki.langs enables and every language in shiki.customLangs. A language outside that set renders as plain text. The client build contains grammars only for that set, and each one loads on demand the first time a block needs it. The page shares one highlighter.

Custom themes

shiki.themes.light and shiki.themes.dark each accept either the name of a bundled Shiki theme, as above, or a full Shiki theme object. Use an object when a bundled theme does not colour a scope the way your site needs, for example the scopes of a custom language. Extend a bundled theme by spreading it and adding tokenColors:

typescript
import githubLight from "@shikijs/themes/github-light";
import githubDark from "@shikijs/themes/github-dark";

const accent = (name: string, foreground: string) => ({
  scope: [name],
  settings: { foreground },
});

export default {
  shiki: {
    themes: {
      light: {
        ...githubLight,
        name: "site-light",
        tokenColors: [...githubLight.tokenColors, accent("keyword.control.demo", "#7c3aed")],
      },
      dark: {
        ...githubDark,
        name: "site-dark",
        tokenColors: [...githubDark.tokenColors, accent("keyword.control.demo", "#a78bfa")],
      },
    },
  },
};

A theme object is a Shiki theme registration: a name, a type ("light" or "dark"), colors, and tokenColors (or settings). Light and dark can mix a name with an object.

  • Theme objects must be plain JSON data. Folio loads them on the server while compiling MDX and, lazily, in CodeBlock components that highlight code in the browser; they are kept out of the initial client bundle.
  • CodeBlock components that highlight in the browser use a theme object for each mode that has one and the default github-light and github-dark themes for a mode configured by name.
  • Theme names behave exactly as before.

Folio validates shiki.themes when the configuration loads and stops with a [folio] error for a value that is neither a name nor an object, an object without a non-empty name, a name that matches a bundled theme (such as github-light), light and dark objects that share a name, a type other than light or dark, or tokenColors, settings or colors of the wrong shape:

text
[folio] shiki.themes.light ("github-light") uses "github-light", which is a bundled Shiki theme name; choose a different name

Custom languages

Use shiki.customLangs to highlight languages Folio does not bundle. Each entry is a Shiki language registration: a TextMate grammar with a name, a scopeName, patterns, and optional aliases.

typescript
import { demoGrammar } from "./src/demo-grammar.ts";

export default {
  shiki: {
    customLangs: [demoGrammar],
  },
};
typescript
// src/demo-grammar.ts
export const demoGrammar = {
  name: "demo",
  scopeName: "source.demo",
  aliases: ["dm"],
  patterns: [{ name: "keyword.control.demo", match: "\\b(?:fn|let)\\b" }],
};

A fenced block whose language matches a registered name or alias, such as demo or dm, is highlighted with the grammar in the configured themes and labelled with the language name. Matching is case-insensitive.

  • Custom languages are not limited by shiki.langs, which only filters built-in languages.
  • Built-in languages and unknown languages behave as before; unknown languages render as plain text.
  • Grammars must be plain JSON data. They are loaded on demand and are kept out of the initial client bundle.
  • CodeBlock components that highlight code in the browser use the same registrations, with the default github-light and github-dark themes unless the site configures custom themes.

Folio validates the registrations when the configuration loads and stops with a [folio] error for a missing name, scopeName, or patterns array, a name or alias that matches a built-in language or alias (for example typescript or sh), or a name, alias, or scopeName registered more than once:

text
[folio] shiki.customLangs[1] ("typescript") uses "typescript", which is a built-in language or alias

Code tabs

CodeTabs shows several code fences as tabs in one code block. Put the fences inside the component and label each one with title="..." in the fence meta:

text
<CodeTabs syncKey="language">

```ts title="TypeScript"
const answer: number = 42;
```

```js title="JavaScript"
const answer = 42;
```

</CodeTabs>

Leave a blank line between the tags and the fences. A fence without a title is labelled with its language, and two tabs of one group cannot have the same label.

Each tab keeps the language you gave its fence, so highlighting is exactly what a standalone fence would get. Nothing is rewritten, no $ prompt is added, and the copy button copies the code of the active tab. The tab list follows the keyboard conventions of the ARIA tabs pattern: Tab moves into the list, and the arrow keys, Home, and End move between tabs.

Syncing tabs

Give groups the same syncKey and they switch together. Choose a tab in this group and the one below follows:

The choice is remembered in the browser's localStorage under folio:code-tabs:<syncKey>, so it also applies on other pages and after a reload. A group that has no tab with the remembered label shows its first tab and leaves the stored choice alone. The first tab is also what the server renders and what shows when storage is unavailable. Groups without a syncKey switch on their own and remember nothing.

CodeTabs is copied into your project by folio init as src/components/ui/code-tabs.tsx. In a project that was created earlier, copy that file from the Folio package to use it. PackageManagerTabs is a separate component for package manager commands and does not use it.