Expressive Code’s default syntax highlighter is Shiki, which uses the same engine as VS Code to provide accurate syntax highlighting for over 100 languages.
Usage in markdown / MDX
Regular syntax highlighting
To get syntax highlighting for your code blocks, wrap them in code fences (using three or more backticks) and ensure that your opening code fences include a language identifier, e.g. js for JavaScript:
The rendered result looks like this:
Rendering ANSI escape sequences
Expressive Code supports rendering a subset of ANSI escape sequences. This can be useful for displaying formatted terminal output in your documentation.
To enable this feature for a code block, set its language identifier in the opening code fence to ansi. You can then use the following ANSI escape sequences:
Result:
Supported languages
Out of the box, over 100 languages are supported, including JavaScript, TypeScript, HTML, CSS, Astro, Markdown, MDX, JSON, YAML, and many more. You can find a list of all language identifiers on GitHub.
Differences to Shiki’s HTML output
If you’re migrating an existing site to Expressive Code that uses custom CSS to modify syntax-highlighted code, please note that the HTML output and classes generated by Expressive Code do not match the default output generated by Shiki.
Instead, the syntax tokens generated by Shiki are converted to Expressive Code’s own annotations (InlineStyleAnnotation), which provide efficient multi-theme support and can be combined with other annotations.
If you need additional classes on some HTML elements, consider writing your own plugin to modify the output as needed.
Configuration
When using this plugin through a framework integration, you can configure it by passing options to the integration.
Here are configuration examples for common scenarios:
By default, the additional languages defined in langs are only available in top-level code blocks contained directly in their parent Markdown or MDX document.
Setting this option to true also enables syntax highlighting when a fenced code block using one of your additional langs is nested inside an outer markdown, md or mdx code block. Example:
transformers
Type:ShikiTransformer[]Default:[]
An optional list of Shiki transformers that should be called during syntax highlighting.
This option allows you to add transformers that modify the tokens produced by Shiki to improve syntax highlighting, e.g. by applying bracket matching or changing the color of certain tokens.
Using another syntax highlighter
If you want to use another syntax highlighter, you can set shiki: false in the configuration to prevent the default highlighter from being loaded. You can then write a plugin for the new syntax highlighter and add it to the plugins array.