🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Content & MDX••By Komal Nakrani

Astro Shiki Syntax Highlighting: Themes and Setup

Astro Shiki syntax highlighting explained: the default theme, shikiConfig options, light and dark dual themes, the Code component, transformers, and when to use Prism.

#Astro#Astro Shiki#Syntax Highlighting#Markdown#MDX

Astro highlights code with Shiki by default: every fenced code block in Markdown and MDX is tokenised at build time and output as HTML with inline colours, using the github-dark theme unless you configure another. You change themes, add dual light and dark themes, register languages and add transformers through markdown.shikiConfig in astro.config.mjs, and you can highlight code in .astro files with the built-in <Code /> component.

Because Shiki runs on the server, highlighted code ships no JavaScript and no highlighter library to the browser. This guide covers how it works in Astro 7 and every common configuration.

Key Takeaways

  • Shiki is on by default for Markdown and MDX; the default theme is github-dark.
  • Configure it with markdown.shikiConfig: theme, themes, defaultColor, langs, langAlias, wrap and transformers.
  • Dual themes output CSS variables, so a few lines of CSS switch code colours with your site’s dark mode.
  • Use <Code /> from astro:components to highlight strings in .astro files.
  • Shiki transformers add line highlights, diffs and focus markers without client JavaScript.

How Shiki works in Astro

Shiki uses the same TextMate grammars and themes as VS Code, so code looks the way it does in an editor. During the Markdown pipeline, Astro finds each fenced block, reads its language tag, runs Shiki, and replaces the block with a <pre class="astro-code"> element where every token is a <span> with an inline colour.

Three consequences matter in practice:

  1. No runtime cost. The work happens at build time (or at request time for on-demand routes). Visitors download plain HTML.
  2. Accurate highlighting. Shiki ships grammars for a very wide range of languages, including astro, mdx, tsx, vue and svelte.
  3. Language tags matter. A block without a tag, or with an unknown one, is output as plain text. Always tag your blocks: ```ts, ```astro, ```bash.

Syntax highlighting sits in the processor stage of the Markdown pipeline and applies whether you use Astro 7’s default Sätteri processor or unified(). Our explainer on how Astro turns Markdown into HTML shows where it fits.

Configuring shikiConfig

All options live under markdown.shikiConfig:

// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
  markdown: {
    shikiConfig: {
      theme: 'github-light',
      langAlias: { cjs: 'javascript', zsh: 'bash' },
      wrap: true,
    },
  },
});
Option Purpose
theme One theme name or theme object, such as 'dracula', 'nord', 'github-light'
themes Several named themes, for light and dark output
defaultColor Which of themes is applied by default, or false for CSS variables only
langs Extra or custom grammars
langAlias Map your own tags to Shiki languages
wrap true to wrap long lines, false to scroll, null to remove overflow styles
transformers Shiki transformers that edit the output tree

The MDX integration uses the same settings, so one configuration covers .md and .mdx. Browse the full list of theme names in the Shiki themes gallery.

Light and dark dual themes

Pass themes instead of theme and Shiki writes the default theme’s colours inline plus CSS variables for the others:

// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
  markdown: {
    shikiConfig: {
      themes: {
        light: 'github-light',
        dark: 'github-dark',
      },
    },
  },
});

Each token now carries --shiki-dark and related variables. Switch to them with CSS that matches your site’s dark mode. For a system preference:

/* src/styles/global.css */
@media (prefers-color-scheme: dark) {
  .astro-code,
  .astro-code span {
    color: var(--shiki-dark) !important;
    background-color: var(--shiki-dark-bg) !important;
    font-style: var(--shiki-dark-font-style) !important;
    font-weight: var(--shiki-dark-font-weight) !important;
    text-decoration: var(--shiki-dark-text-decoration) !important;
  }
}

For a class or attribute toggle, swap the media query for a selector such as [data-theme='dark'] .astro-code. The !important is needed because the light colours are inline styles. If you prefer no inline colours at all, set defaultColor: false and style both themes through their variables (--shiki-light and --shiki-dark). The dual themes tip has a complete toggle example.

Highlighting code in .astro files with Code

Markdown is not the only place code appears. The <Code /> component highlights any string with the same engine:

---
// src/components/ConfigSnippet.astro
import { Code } from 'astro:components';

const snippet = `export default defineConfig({
  site: 'https://example.com',
});`;
---
<Code code={snippet} lang="js" themes={{ light: 'github-light', dark: 'github-dark' }} wrap />

<Code /> takes code, lang, theme or themes, defaultColor, wrap, inline, meta, embeddedLangs and transformers. Two props are easy to miss: inline renders a <code> element without the <pre> wrapper, for highlighted code inside a sentence, and embeddedLangs loads extra grammars when one language contains another, such as TSX inside a Vue file.

The component does not read markdown.shikiConfig, so pass the theme props you want, or wrap it in your own component with your defaults set once.

Line highlights, diffs and focus with transformers

Transformers modify Shiki’s output tree. The @shikijs/transformers package covers the common documentation patterns:

# Terminal
npm install @shikijs/transformers
// astro.config.mjs
import { defineConfig } from 'astro/config';
import {
  transformerNotationDiff,
  transformerNotationHighlight,
  transformerNotationFocus,
} from '@shikijs/transformers';

export default defineConfig({
  markdown: {
    shikiConfig: {
      theme: 'github-dark',
      transformers: [
        transformerNotationDiff(),
        transformerNotationHighlight(),
        transformerNotationFocus(),
      ],
    },
  },
});

Now comments inside code control the output. // [!code ++] and // [!code --] mark added and removed lines, // [!code highlight] highlights a line, and // [!code focus] dims everything else. The transformers add classes such as highlighted, diff add and focused; you supply the CSS:

/* src/styles/code.css */
.astro-code .line.highlighted { background: rgb(255 255 255 / 0.08); }
.astro-code .line.diff.add { background: rgb(46 160 67 / 0.2); }
.astro-code .line.diff.remove { background: rgb(248 81 73 / 0.2); }
.astro-code.has-focused .line:not(.focused) { opacity: 0.5; }

The line highlighting tip covers range syntax and styling in more detail. You can pass the same transformers array to <Code />.

Excluding languages and switching to Prism

Some code fences are not code to highlight. Mermaid and D2 diagrams, for example, are rendered by another tool. Exclude them so Shiki leaves the raw text in place:

// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
  markdown: {
    syntaxHighlight: {
      type: 'shiki',
      excludeLangs: ['mermaid', 'math'],
    },
  },
});

math is excluded by default. To use Prism instead, set syntaxHighlight: 'prism' and add a Prism theme stylesheet yourself; Prism outputs classes rather than inline colours. Set syntaxHighlight: false to turn highlighting off entirely, which is useful if you highlight on the client or with a separate tool.

Shiki (default) Prism
Output Inline styles or CSS variables Classes; you add a stylesheet
Themes VS Code themes, dual themes Prism CSS themes
Grammar accuracy TextMate grammars Regex-based grammars
Component <Code /> from astro:components <Prism /> from @astrojs/prism

Shiki is the better default for most sites. Prism makes sense if you already have a Prism theme or need class-based output for a strict content security policy.

Expressive Code and Starlight

If you want frames with file names, terminal windows, copy buttons and line markers in one package, Expressive Code builds on Shiki and integrates with Astro. Starlight uses it by default. For a plain blog, Shiki plus a couple of transformers and a small copy code button covers most needs with fewer moving parts.

Performance notes

Shiki loads grammars and themes during the build, so large documentation sites with many languages build a little slower. Keep langs and themes to the ones you use, and prefer one dual-theme setup over several separate themes. The output size depends on the number of tokens; long code blocks produce long HTML, which compresses well. The official syntax highlighting guide lists every option. Most Astro themes with a blog ship a Shiki configuration you can adjust in astro.config.mjs.

FAQ

What syntax highlighter does Astro use?

Astro uses Shiki by default for Markdown and MDX code blocks, with the github-dark theme. Highlighting happens on the server and ships no JavaScript. You can switch to Prism with markdown.syntaxHighlight: 'prism' or turn highlighting off with false.

How do I change the Shiki theme in Astro?

Set markdown.shikiConfig.theme in astro.config.mjs to any Shiki theme name, such as 'nord' or 'github-light', or pass a custom theme object. For light and dark versions, use themes with light and dark keys.

How do I add dark mode to Astro code blocks?

Configure shikiConfig.themes with a light and a dark theme, then add CSS that applies the --shiki-dark variables to .astro-code and its spans under your dark mode selector or prefers-color-scheme: dark media query.

How do I highlight code in an .astro component?

Import Code from astro:components and pass the string to its code prop with a lang, for example <Code code={snippet} lang="ts" />. Set theme or themes on the component, because it does not read markdown.shikiConfig.

Why is my Astro code block not highlighted?

Usually the fence has no language tag or an unrecognised one. Add a tag such as ts or astro, or map your tag with langAlias. Also check that markdown.syntaxHighlight is not set to false and that the language is not in excludeLangs.