Adding Code Previews To Astro

This are the steps I took to add code previews to my .mdx files in Astro.

Flo

Necessary steps

  1. Install the expressive code plugin
  2. Update your astro.config.mjsfile so that expressiveCode() comes before mdx() (Source)
  3. Use a code block in your .mdx files
  4. Add the line numbers plugin (make sure to put the required ec.config.mjs on the same folder level as astro.config.mjs)
  5. Optional: Override some styles
    astro.config.mjs
    // @ts-check
    import { defineConfig } from "astro/config";
    import preact from "@astrojs/preact";
    import tailwindcss from "@tailwindcss/vite";
    import mdx from "@astrojs/mdx";
    import astroExpressiveCode from "astro-expressive-code";
    // https://astro.build/config
    export default defineConfig({
    site: "https://example.com",
    integrations: [
    astroExpressiveCode({
    styleOverrides: {
    borderRadius: '0.5rem',
    frames: {
    shadowColor: '#124',
    },
    codePaddingBlock: '0rem',
    codePaddingInline: '0rem',
    uiPaddingBlock: '0rem',
    }}),
    preact(),
    mdx()
    ],
    vite: {
    plugins: [tailwindcss()],
    },
    });