Nextra uses Shiki to apply syntax highlighting at build time, producing beautifully colored code blocks with zero runtime overhead. Because highlighting happens during the build step, your pages load fast and there is no flash of unstyled code. Simply add a fenced code block with a language tag and Nextra handles the rest.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/shuding/nextra/llms.txt
Use this file to discover all available pages before exploring further.
Basic Usage
Add a language identifier after the opening triple backticks:Features
Inline Code Highlighting
Highlight inline code snippets with a language using the{:language} suffix:
{:jsx} suffix tells Shiki which grammar to apply to that specific inline token.
Line Highlighting
Highlight specific lines by adding a{} attribute with line numbers or ranges:
4-5) and individual lines (1,3) can be combined freely.
Word / Substring Highlighting
Highlight every occurrence of a substring using the/word/ attribute:
/str/1, /str/1-3, or /str/1,3.
Copy Button
Add acopy attribute to display a copy-to-clipboard button when the user hovers over the block:
You can enable the copy button globally for every code block by setting
defaultShowCopyCode: true in your Nextra configuration in
next.config.mjs. Once enabled globally, disable it on individual blocks
with copy=false.Line Numbers
Add theshowLineNumbers attribute to display a gutter with line numbers:
Filenames and Titles
Label a code block with a filename or title using thefilename attribute:
Focus Mode
Use// [!code focus] on specific lines to dim all other lines and draw attention to the focused code:
Diff Annotations
Mark lines as added or removed using// [!code ++] and // [!code --]:
ANSI Highlighting
Use theansi language tag to highlight terminal output with ANSI escape codes:
Custom Grammar
Shiki accepts VSCode TextMate Grammars for custom languages. OverridegetHighlighter inside mdxOptions.rehypePrettyCodeOptions in your Nextra config:
Custom Themes
Pass a custom theme or a VSCode theme JSON file viarehypePrettyCodeOptions:
- Single Theme
- Dark & Light Mode
npm2yarn — Package Manager Tabs
Nextra ships the@theguild/remark-npm2yarn remark plugin. Add the npm2yarn metadata flag to any sh code block and Nextra will automatically render it as a tabbed component showing the equivalent npm, yarn, and pnpm commands:
localStorage so users only have to choose their package manager once across the whole site.
Disabling Syntax Highlighting
To opt out of Shiki entirely and use your own highlighting library, setcodeHighlight: false in your Nextra config:
| Option | Type | Default | Description |
|---|---|---|---|
codeHighlight | boolean | true | Enable or disable Shiki syntax highlighting. |
defaultShowCopyCode | boolean | — | Show the copy button on all code blocks by default (disable with copy=false). |
mdxOptions.rehypePrettyCodeOptions | object | {} | Options forwarded to rehype-pretty-code for custom themes and grammars. |
TwoSlash (TypeScript Interactive Docs)
Nextra supports TwoSlash for TypeScript code blocks, enabling inline type information and compiler diagnostics directly in your docs. Configure it viarehypePrettyCodeOptions in next.config.mjs following the Shiki TwoSlash documentation.