Adding code samples
You can add inline code snippets or code blocks. Code blocks support meta options for syntax highlighting, titles, line highlighting, icons, and more.
Inline code
To denote a word or phrase as code, enclose it in backticks (`).
Code blocks
Use fenced code blocks by enclosing code in three backticks. Code blocks are copyable, and if you have the assistant enabled, users can ask AI to explain the code.
Specify the programming language for syntax highlighting and to enable meta options. Add any meta options, like a title or icon, after the language.
Code block options
Add meta options to your code blocks to customize their appearance.
You must specify a programming language for a code block before adding any other meta options.
Option syntax
- String and boolean options: Wrap with
"",'', or no quotes. - Expression options: Wrap with
{},"", or''.
Syntax highlighting
Enable syntax highlighting by specifying the programming language after the opening backticks of a code block.
We use Shiki for syntax highlighting and support all available languages. See the full list of languages in Shiki's documentation.
Customize code block themes globally using styling.codeblocks in your config.json file. Set simple themes like system or dark, or configure custom Shiki themes for light and dark modes. See Settings for configuration options.
Twoslash
In JavaScript and TypeScript code blocks, use twoslash to enable interactive type information. Users can hover over variables, functions, and parameters to see types and errors like in an IDE.
Title
Add a title to label your code example. Use title="Your title" or a string on a single line.
Icon
Add an icon to your code block using the icon property. See Icons for all available options.
Line highlighting
Highlight specific lines using the <CodeBlock> component with the highlight prop. Specify line numbers and ranges like "{1,3-5}".
Line focusing
Focus on specific lines using the <CodeBlock> component with the focus prop. Non-focused lines will be dimmed.
Show line numbers
Display line numbers on the left side of your code block using the <CodeBlock> component with the lines prop.
Expandable
Allow users to expand and collapse long code blocks using the <CodeBlock> component with the expandable prop.
Wrap
Enable text wrapping for long lines using the <CodeBlock> component with the wrap prop. This prevents horizontal scrolling and makes long lines easier to read.
Diff
Show a visual diff of added or removed lines in your code blocks. Added lines are highlighted in green and removed lines are highlighted in red.
Use the <CodeBlock> component with the diff prop to auto-detect lines starting with + or -:
Or use the add and remove props with line numbers for explicit control:
CodeBlock component
Use the <CodeBlock> component in custom React components to programmatically render code blocks with the same styling and features as markdown code blocks.
Props
The icon to display in the code block header. See Icons for available options.
The lines to mark as added (green). Supports individual lines and ranges. Example: "{1,3-5}".
The lines to mark as removed (red). Supports individual lines and ranges. Example: "{2,4}".