Write your code like a decent human being – how to add Rehype to your next Astro project
Have you ever looked at a block of code and wanted to gouge your freaking eyes out?
No? Yeah, me neither.
I’ve been using rehype-pretty-code
, cause I’m not an idiot.
If you’re still presenting your code like an ape with just the basic styling, then I’m sorry – you’re an idiot.
BUT, worry no more, cause today I’ll show you how with just a couple of lines of code you can stop being one (an idiot, I mean).
Introducing Rehype
The hype with Rehype (I know, genius) is understandable – with just a few lines of code you can present your code like Picasso (before that Abstraction nonsense).
Let’s make your code look pretty in no time!
1. Install the plugin
npm install rehype-pretty-code shiki
(the library is powered by shiki
syntax highlighter)
If you’re interested how it works under the hood you can take a look at the docs.
2. Adjust the astro.config
file
At a minimum, you have to add the plugin (duh) and disable Astro’s default syntax highlighting.
Now, if you inspect your code blocks, you should see rehype syntax added.
3. Sprinkle some CSS
By design, rehype
doesn’t come with any sort of styling, so you can freely activate your inner Rembrandt (I guess we’re also learning about painting today).
You should probably do that without the rehype
plugin, so let’s first add some base styling if you’re really starting from scratch (I’ll be using some random dark-themed colors, so feel free to adjust it to your liking).
Now, copy and paste the below rehype
specific CSS and you should be good to go:
Sooooo, if you didn’t mess anything up (just kidding, I love you), your code should look something like this: