Syntax highlighting
Configure Shiki themes, custom theme objects, the language allowlist, and custom languages.
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:
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
CodeBlockcomponents that highlight code in the browser; they are kept out of the initial client bundle. CodeBlockcomponents that highlight in the browser use a theme object for each mode that has one and the defaultgithub-lightandgithub-darkthemes 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:
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.
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.
CodeBlockcomponents that highlight code in the browser use the same registrations, with the defaultgithub-lightandgithub-darkthemes 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:
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:
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.