Skip to content

Syntax Highlighting

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:

example.md
```js
console.log('This code is syntax highlighted!')
```

The rendered result looks like this:

console.log('This code is syntax highlighted!')

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:

ansi-example.md
```ansi
ANSI colors:
- Regular: Red Green Yellow Blue Magenta Cyan
- Bold: Red Green Yellow Blue Magenta Cyan
- Dimmed: Red Green Yellow Blue Magenta Cyan
256 colors (showing colors 160-177):
160 161 162 163 164 165
166 167 168 169 170 171
172 173 174 175 176 177
Full RGB colors:
ForestGreen - RGB(34, 139, 34)
Text formatting: Bold Dimmed Italic Underline
```

Result:

Terminal window
ANSI colors:
- Regular: Red Green Yellow Blue Magenta Cyan
- Bold: Red Green Yellow Blue Magenta Cyan
- Dimmed: Red Green Yellow Blue Magenta Cyan
256 colors (showing colors 160-177):
160 161 162 163 164 165
166 167 168 169 170 171
172 173 174 175 176 177
Full RGB colors:
ForestGreen - RGB(34, 139, 34)
Text formatting: Bold Dimmed Italic Underline

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:

astro.config.mjs
import { defineConfig } from 'astro/config'
import astroExpressiveCode from 'astro-expressive-code'
// Add this if you want to load a custom language grammar from a file:
// import fs from 'node:fs'
export default defineConfig({
integrations: [
astroExpressiveCode({
// You can use any of the themes bundled with Shiki by name,
// specify a path to JSON theme file, or pass an instance
// of the `ExpressiveCodeTheme` class here:
themes: ['dracula', 'solarized-light'],
shiki: {
// You can pass additional plugin options here,
// e.g. to load custom language grammars:
langs: [
// import('./some-exported-grammar.mjs'),
// JSON.parse(fs.readFileSync('./some-json-grammar.json', 'utf-8'))
],
},
}),
],
})

Available plugin options

You can pass the following options to the plugin:

bundledLangs

Type: BundledShikiLanguage[] Default: undefined Availability: Astro and Starlight integrations only

Allows defining a subset of language IDs from the full Shiki bundle that should be available for syntax highlighting.

In server-side rendering (SSR) environments, setting this option to the languages used on your site can reduce bundle size by up to 80%.

If this option is not set, all languages from the full Shiki bundle are available.

astro.config.mjs
import { defineConfig } from 'astro/config'
import astroExpressiveCode from 'astro-expressive-code'
import cloudflare from '@astrojs/cloudflare'
export default defineConfig({
integrations: [
astroExpressiveCode({
shiki: {
// Example: Only include languages 'astro' and 'sass'
// in the bundle, reducing SSR bundle size by 80%
bundledLangs: ['astro', 'sass'],
},
}),
],
// Server-side rendering (SSR) must be enabled
// for bundle size trimming to have an effect
output: 'server',
adapter: cloudflare(),
})

engine

Type: 'oniguruma' | 'javascript' Default: 'oniguruma'

The Shiki RegExp engine to be used for syntax highlighting. The following options are available:

  • 'oniguruma': The default engine that supports all grammars, but requires a target environment with WebAssembly (WASM) support.
  • 'javascript': A pure JavaScript engine that does not require WASM. The Shiki team is continuously improving this engine and aims for full compatibility with the Oniguruma engine. Use this engine if your target environment does not support WASM.

langs

Type: LanguageInput[] Default: []

A list of additional languages that should be available for syntax highlighting.

You can pass any of the language input types supported by Shiki, e.g.:

  • import('./some-exported-grammar.mjs')
  • JSON.parse(fs.readFileSync('./some-json-grammar.json', 'utf-8'))

See the Shiki documentation for more information.

langAlias

Type: Record<string, string>

Allows defining alias names for languages. The keys are the alias names, and the values are the language IDs to which they should resolve.

The values can either be bundled languages, or additional languages defined in langs.

astro.config.mjs
import { defineConfig } from 'astro/config'
import astroExpressiveCode from 'astro-expressive-code'
export default defineConfig({
integrations: [
astroExpressiveCode({
shiki: {
// Allow using the alias 'mjs' for the 'javascript' language
langAlias: {
mjs: 'javascript',
},
},
}),
],
})

injectLangsIntoNestedCodeBlocks

Type: boolean Default: false

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:

````md
This top-level Markdown code block contains a nested `my-custom-lang` code block:
```my-custom-lang
This nested code block will only be highlighted using `my-custom-lang`
if `injectLangsIntoNestedCodeBlocks` is enabled.
```
````

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.