Skip to main content

Add 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 the assistant 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. Mintlify uses Shiki for syntax highlighting and supports all available languages. See the full list of languages in Shiki’s documentation. Customize code block themes globally using styling.codeblocks in your docs.json file. Set simple themes like system or dark, or configure custom Shiki themes for light and dark modes. See Settings for configuration options.
For custom themes, set your theme in docs.json to "css-variables" and override syntax highlighting colors using CSS variables with the --mint- prefix.The following variables are available:Basic colors
  • --mint-color-text: Default text color
  • --mint-color-background: Background color
Token colors
  • --mint-token-constant: Constants and literals
  • --mint-token-string: String values
  • --mint-token-comment: Comments
  • --mint-token-keyword: Keywords
  • --mint-token-parameter: Function parameters
  • --mint-token-function: Function names
  • --mint-token-string-expression: String expressions
  • --mint-token-punctuation: Punctuation marks
  • --mint-token-link: Links
ANSI colors
  • --mint-ansi-black, --mint-ansi-black-dim
  • --mint-ansi-red, --mint-ansi-red-dim
  • --mint-ansi-green, --mint-ansi-green-dim
  • --mint-ansi-yellow, --mint-ansi-yellow-dim
  • --mint-ansi-blue, --mint-ansi-blue-dim
  • --mint-ansi-magenta, --mint-ansi-magenta-dim
  • --mint-ansi-cyan, --mint-ansi-cyan-dim
  • --mint-ansi-white, --mint-ansi-white-dim
  • --mint-ansi-bright-black, --mint-ansi-bright-black-dim
  • --mint-ansi-bright-red, --mint-ansi-bright-red-dim
  • --mint-ansi-bright-green, --mint-ansi-bright-green-dim
  • --mint-ansi-bright-yellow, --mint-ansi-bright-yellow-dim
  • --mint-ansi-bright-blue, --mint-ansi-bright-blue-dim
  • --mint-ansi-bright-magenta, --mint-ansi-bright-magenta-dim
  • --mint-ansi-bright-cyan, --mint-ansi-bright-cyan-dim
  • --mint-ansi-bright-white, --mint-ansi-bright-white-dim
Custom syntax highlightingAdd syntax highlighting for languages not included in Shiki’s default set by providing custom TextMate grammar files. Create a JSON file following the TextMate grammar format, then reference it in your docs.json. You can add multiple custom languages by including additional paths in the array.
docs.json

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. Place the title after the language identifier. Titles can contain multiple words and file paths. You can set the title two ways:
  • Inline: Place the title directly after the language identifier.
  • title property: Use title="Your title" for titles that need special characters or explicit quoting.

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 focusing

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

Show line numbers

Display line numbers on the left side of your code block using lines.

Expandable

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

Wrap

Enable text wrapping for long lines using wrap. 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. Add [!code ++] or [!code --] inside a comment at the end of a line to mark it as added or removed. Use your language’s comment syntax:
LanguageAddedRemoved
JavaScript, TypeScript, Java, C, C++, Go, Rust// [!code ++]// [!code --]
Python, Ruby, Bash, YAML# [!code ++]# [!code --]
HTML, XML<!-- [!code ++] --><!-- [!code --] -->
CSS/* [!code ++] *//* [!code --] */
SQL, Lua-- [!code ++]-- [!code --]
For multiple consecutive lines, add a colon and the number of lines:
  • // [!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.

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

language
string
The programming language for syntax highlighting.
filename
string
The filename to display in the code block header.
icon
string
The icon to display in the code block header. See Icons for available options.
lines
boolean
Whether to show line numbers.
wrap
boolean
Whether to wrap the code block.
expandable
boolean
Whether to expand the code block.
highlight
string
The lines to highlight. Provide a stringified array of numbers. Example: "[1,3,4,5]".
focus
string
The lines to focus on. Provide a stringified array of numbers. Example: "[1,3,4,5]".

Example