Code blocks
DocsLit provides enhanced code blocks with syntax highlighting, filenames, copy buttons, tabbed groups, and editable variables.
Basic code block
Style a code block's parts with ::part(): root, header and copy-button. A code group has root, tab-bar, tab-content and copy-button. See Custom CSS.
Use standard fenced code blocks with a language identifier:
Every code block includes a copy button automatically.
Syntax highlighting
DocsLit uses Shiki for build-time syntax highlighting. Code blocks are highlighted at parse time, so there is no client-side rendering cost. Shiki supports dual themes -- your code blocks automatically match the page's light or dark mode.
Code theme toggle
Every code block and code group includes a kebab menu that lets readers switch all code blocks between light and dark mode. This setting is independent of the page theme and is saved to the browser for future visits.
You can also force a specific theme on any individual code block:
Fence options
Everything after the language in the opening fence is read as options. The same options exist as attributes on the wc-code-block element, so you can write whichever form you like.
| Fence | Attribute | Effect |
|---|---|---|
title="app.js", filename="app.js", or bare words after the language |
filename |
Header title |
icon=code |
icon |
Icon in the header (a wc-icon name) |
{1,3-5} or highlight={1,3-5} |
highlight="1,3-5" |
Highlight lines |
focus=2,4-5 |
focus="2,4-5" |
Dim every other line |
lines or linenumbers="true" |
linenumbers="true" |
Line numbers |
startLine={10} |
start-line="10" |
First line number |
wrap or wordWrap |
wrap |
Wrap long lines |
expandable |
expandable |
Collapse long blocks behind "Show more" (10 lines) |
maxLines=5 |
max-lines="5" |
Collapse after N lines |
annotate |
annotate |
Turn (1) comment markers into numbered notes (see Code annotations) |
nocopy |
nocopy |
Hide the copy button |
for="npm" |
for="npm" |
Sync token inside a code group |
Mintlify and Fern spellings are accepted as they are, so content moved from those platforms keeps working.
Title and icon
A fence with only a language still shows a small header with the language name and the copy button. A fence with no language and no title gets a copy button that appears on hover.
Line highlighting
Use braces or highlight=. Ranges and lists mix freely.
Focus
focus dims the other lines. Hovering or focusing the block brings them back, and touch devices keep the dimming.
Diff, highlight and focus comments
Add a comment at the end of a line to mark it. This is the Shiki notation, so the same markers work in code copied from Mintlify or Fern docs. The marker comment is removed from the display and from the copy button.
| Marker | Effect |
|---|---|
[!code ++] |
Added line (green, + in the gutter) |
[!code --] |
Removed line (red, minus in the gutter) |
[!code highlight] |
Highlighted line |
[!code focus] |
Focused line |
[!code error], [!code warning] |
Error or warning line |
[!code ++:3] |
This line and the next two |
Use the comment syntax of the language (//, #, --, /* */, <!-- -->). A marker on a line of its own is removed and applies to the lines after it.
Languages without comments (text, markdown, json) are never scanned, so you can show the notation itself in a text block as above. The diff language is rendered as a diff too, and keeps its + and - signs when copied:
Line numbers
Numbers are drawn by CSS, so they are never selected or copied.
Wrapping and collapsing
Long lines scroll horizontally by default; add wrap to wrap them. Long blocks can collapse behind a "Show more" button (a real button with aria-expanded):
expandable alone collapses after 10 lines. Blocks shorter than the limit are never collapsed.
Hide the copy button
What copying does
The copy button copies the code as written: no line numbers, no gutter signs, no [!code ...] markers, and {{VARIABLES}} replaced by their current values. In a diff block the + and - signs are part of the code and are kept.
Importing code
Options combine with code import. Fence options other than the import keys (file, lines, region) are passed through:
Generated Markdown and llms.txt
The .md pages and llms.txt contain the code without presentation markers: [!code highlight], focus, error and warning comments are removed, and a block that uses [!code ++] / [!code --] is written as a diff block with + and - prefixes.
Twoslash
twoslash (interactive type hovers) is not supported. The flag is accepted and ignored, so the block renders as normal highlighted code.
Code block with filename
Add a filename attribute to show a file header:
Hand-written blocks are highlighted at build time just like fences, and support every option as an attribute:
javascript, python, bash, json, html, css. Shown as a label in the header.true or false. Set to true to show line numbers.1.1,3-5.expandable).dark or light. Pins the block to a color scheme.Code annotations
Code annotations attach numbered notes to specific lines. Add the annotate flag to the fence, end a comment on the line with a marker, and write the notes as an ordered list directly after the block.
(1),(2), ... at the end of a comment marks the line. The marker is replaced by a numbered badge and the comment stays visible.(1)!hides the comment characters too, so only the badge remains.- The ordered list right after the block holds the notes. Item N is the note for marker
(N). Notes can contain inline Markdown such as code, links and emphasis.
- Only network errors and
5xxresponses are worth retrying. - Always set a timeout, or a stalled server can hang the call forever.
- Callers should catch
RuntimeErrorand report the URL.
How it behaves
- Clicking a badge opens its note in a popover next to the line. Clicking it again, clicking elsewhere or pressing
Escapecloses it, and focus returns to the badge. Opening one note closes any other open note. - Badges are real buttons, so they can be reached with
Taband opened withEnterorSpace. - Markers are removed from the copy button, so copied code is clean.
- Without JavaScript the badges are static and the ordered list stays visible under the code, so the notes are never lost.
- Markers work with the other fence options, for example
annotate lines {2}.
Build warnings
docslit validate and docslit build report these problems. None of them stops the build.
| Code | Cause |
|---|---|
code-annotation-no-list |
The block has annotate but no ordered list directly after it. Markers stay as plain text. |
code-annotation-no-marker |
The block has annotate but no (N) marker in a comment. The list stays below the code. |
code-annotation-missing-item |
A marker (N) has no matching list item. |
Code groups
Group related code examples in tabs. The simplest form puts fences straight inside wc-code-group; each fence's title (or its language) becomes the tab label:
Each fence keeps its own options (lines, {2}, nocopy, ...). You can also give the tab a label with wc-code-tab. Its content can be a fence, or plain text with a language attribute, which is highlighted at build time:
Dropdown
Add dropdown to replace the tab bar with a native select, handy for long language lists:
Synced groups
Code groups on the same page sync by default. When a reader picks Python in one group, every group that has a Python tab switches too, and the choice is remembered across pages. A tab matches by its for token, its label, or its language (js and javascript are the same language). Opening a page with ?language=python preselects Python.
Use for when the tabs are not about a language, for example package managers. Tabs with the same for value switch together:
Set sync="my-key" to keep a set of groups (and wc-tabs with the same key) separate from the rest, or sync="off" to opt out.
Tab bars follow the WAI-ARIA tabs pattern: Left and Right arrows, Home and End move between tabs.
code; off disables syncing.Editable variables
Use wc-var to create interactive variables that update across all code blocks on the page. Readers click the variable, type their value, and every {{VAR_NAME}} placeholder updates instantly.
Set your API key:
Set your base URL:
Click the variable values above and type your own — watch both code blocks update in real time.
UPPER_SNAKE_CASE by convention. Reference it in code blocks with {{NAME}}.<wc-var name="X" value="Y" /> declares a page-level variable. See Variables and precedence.YOUR_API_KEY", give them a field to type it once and see it everywhere.Copy button on code groups
Code groups include a copy button that copies the active tab's code.
Supported languages
DocsLit uses Shiki for syntax highlighting, which supports all common programming languages including JavaScript, TypeScript, Python, Go, Rust, Java, C, C++, Ruby, PHP, Shell/Bash, SQL, HTML, CSS, JSON, YAML, TOML, Markdown, and more.
Variables and highlighting
Code blocks that contain {{VAR}} placeholders are fully highlighted. The placeholders become editable spans that update as readers change the variable, and copying uses the current values. In the rare case where a placeholder cannot be kept in one piece by the highlighter, the block falls back to plain text so substitution keeps working.