· 4 min read
Astro + Rehype Pretty Code
This an extension on this other guy’s post so read that first if you need to setup everything from scratch.
Background
Astro has been chugging along with new releases at quite a pace and with it came some changes in version 4 regarding plugins. I believe this is what killed my code snippet styling with additional factors from upgrade of shiki
to version 1.0 in the background behind rehype-pretty-code
so all of my original css has needed changing. This post documents the configuration changes needed to make it work again with the latest Astro 4, Tailwind and rehype-pretty-code
plugin.
Configuring Rehype Pretty Code Plugin
My webiste supports dark and light mode code snippets. The new way to support multiple themes is shown below with the imported themes I copied from their respective repo and copy/pasted into my project. Also, I had to update my Tailwind integration’s configuration shown below.
I also found I could remove my previous custom onVisitHightlightedLine
and onVisitHighlightedWord
hooks as this new Shiki version automatically adds data attributes for highlighted lines and characters which I now use in my new css selectors.
Styling Code Snippets with Tailwind
Dark Mode
Again, I followed this blog post becuase it was more clean than my original implementation so I won’t repeat everything but it lacked support for dark mode so I made a few customizations to fit my setup. Firstly, to get dark mode working I added the following to the bottom of my css for code snipets.
Hightlighted Lines
Tailwind adds the dark
class on the root html
element so this snippet changes the Shiki code theme when dark mode is active. There are a few ways to do this based on the shiki style guide but this works for me and we should not include the background-color: var(--shiki-foo)
as that will get in the way of the css that highlights lines of code. Oh, and with this implementation !important
is not required.
Making custom line hightlighting for dark mode is easy since we can still use the regular dark:
syntax.
Background Color
Then I ran into trouble configuring a custom background color for code snippets in dark mode. Eventually, I realized because I’m using Tailwind’s typography plugin my code snippets are within the prose so the css defined by the prose
class which overwrites other background styles. So I added a line with prose-pre
classes to define and now we have our defined dark mode code snippet background.
Group Highlighted Characters
This didn’t break but I wanted to add this as an addition to the aforementioned blog post for how to highlight custom colors based on characters with a specific ID.
In the previous section additional character highlighting styling is configured.
(Mostly) Complete CSS Demo
Besides the parts outside of base.css
mentioned earlier this is the majority of the css for my code snippet styling. It’s mostly specific style preferences I made for myself but I thought it was good to include the full context for this post.
- astro
- tailwind