Adding Code Previews To Astro
This are the steps I took to add code previews to my .mdx files in Astro.
Flo
Necessary steps
- Install the expressive code plugin
- Update your
astro.config.mjsfile so thatexpressiveCode()comes beforemdx()(Source) - Use a code block in your .mdx files
- Add the line numbers plugin (make sure to put the required
ec.config.mjson the same folder level asastro.config.mjs) - Optional: Override some styles
astro.config.mjs // @ts-checkimport { 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/configexport 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()],},});