Getting started
Starlight renders every code block with Expressive Code. The plugin adds its features to those code blocks, so the code blocks you already have keep working. You turn on nothing per page: most features start when a code block uses their attribute or directive. A few, such as file icons and colour swatches, apply to every matching block.
Requirements
Section titled “Requirements”- Astro 7 or later.
- Starlight 0.42 or later.
- Node.js 22.12 or later.
Install the plugin
Section titled “Install the plugin”-
Add the package to the site.
Terminal window npm install starlight-codeblocksTerminal window pnpm add starlight-codeblocksTerminal window yarn add starlight-codeblocks -
Open
astro.config.mjs. -
Add
codeblocks()to thepluginslist of Starlight.astro.config.mjs import starlight from '@astrojs/starlight';import { defineConfig } from 'astro/config';export default defineConfig({integrations: [starlight({title: 'My docs',}),],});
Pick a feature from the sidebar and add its attribute or directive to a code block to turn it on.
Sites with an ec.config.mjs file
Section titled “Sites with an ec.config.mjs file”Some sites keep their Expressive Code options in an ec.config.mjs file. If that file has no plugins list, the plugin works with no change.
If the file has a plugins list, Expressive Code uses that list only. Add pluginCodeblocks() to it:
import { pluginCollapsibleSections } from '@expressive-code/plugin-collapsible-sections';
export default {};Keep codeblocks() in astro.config.mjs as well, and give it the options there. pluginCodeblocks() with no argument uses the same options. If you forget this step, the build stops with a message that tells you what to add.
Sites without Starlight
Section titled “Sites without Starlight”Astro sites that use Expressive Code without Starlight add the preset to the Expressive Code plugins. Give the options to pluginCodeblocks():
export default {};Code tabs, inline code highlighting and the <CodeWalkthrough> and <Scrollycoding> components need Starlight. The features inside code blocks work on every site. Set tabWidth: 0 in the Expressive Code options, so that tabs reach the code blocks unchanged. Blocks with a Run code button need a runtime URL for their language, as in Without Starlight.