Skip to main content

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.
HelloWorld.java

Code block options

You can 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.

Title

Add a title to label your code example. Use title="Your title" or a string on a single line.
Code Block Example

Icon

Add an icon to your code block using the icon property. See Icons for all available options.

Line Highlighting

Highlight specific lines in your code blocks using highlight with the line numbers or ranges you want to highlight.
Line Highlighting Example

Line focusing

Focus on specific lines in your code blocks using focus with line numbers or ranges.
Line Focus Example

Show line numbers

Display line numbers on the left side of your code block using lines.
Show Line Numbers Example

Expandable

Allow users to expand and collapse long code blocks using expandable.
Expandable Example

Wrap

Enable text wrapping for long lines using wrap. This prevents horizontal scrolling and makes long lines easier to read.
Wrap Example

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. To create diffs, add these special comments at the end of lines in your code block:
  • // [!code ++]: Mark a line as added (green highlight).
  • // [!code --]: Mark a line as removed (red highlight).
For multiple consecutive lines, specify the number of lines after a colon:
  • // [!code ++:3]: Mark the current line plus the next two lines as added.
  • // [!code --:5]: Mark the current line plus the next four lines as removed.
The comment syntax must match your programming language (for example, // for JavaScript or # for Python).
Diff Example