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.mjs
file 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.mjs
on 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()],},});
You Might Also Like
Script For Recurring Chores in Home Assistant
This script creates tasks for your chores and sends notifications.
#tutorial
Add Giscus to A Blog In A Private GitHub Repository
Tutorial on how to add Giscus to a blog which is hosted in a private GitHub repository.
#tutorial
Create PNG file from <div>
This post explains how to add a button to a Astro page which downloads a certain <div> element in PNG format.
#astro