- Beautiful Markdown Code Highlighting with Rehype Pretty Code
Syntax highlighting is a must-have for any blog that includes code snippets. But it’s not always easy to get right.
My favourite solution is Rehype Pretty Code. It’s a rehype plugin that adds syntax highlighting to markdown code blocks at build time. It works with any static site generator and supports any VSCode theme.
It builds on the excellent shiki library by adding a few extra features:
- TODO: validate and add the rest
- It can highlight code blocks in any language, not just the ones supported by shiki.
- It can highlight inline code snippets.
How to use it
-
Install the plugin:
npm install rehype-pretty-code
-
Add the plugin to your rehype pipeline:
const rehype = require('rehype') const rehypePrettyCode = require('rehype-pretty-code') rehype() .use(rehypePrettyCode) .process(/* ... */)
-
Add a VS Code theme to your site:
const rehype = require('rehype') const rehypePrettyCode = require('rehype-pretty-code') rehype() .use(rehypePrettyCode, { theme: 'vscode-dark-plus' }) .process(/* ... */)
-
Add a fenced code block to your markdown:
```js const fruit = [ 'apple', 'apple', 'apple' 'orange', 'orange', 'orange', 'banana', 'banana', 'banana', ] ```
-
That’s it! The code block will now be syntax highlighted using the theme you chose.
-
You can also add a title, a caption, highlight lines and/or characters, and show line numbers. See the documentation for more details.
-
If you want to use a different theme, you can find a list of supported themes here.
-
If you want to use a custom theme, you can find instructions here.
-
If you want to use a custom language, you can find instructions here.
-
If you want to use a custom inline syntax highlighting token, you can find instructions here.
-
If you want to use a custom inline syntax highlighting token, you can find instructions here.
-
If you want to use a custom inline syntax highlighting token, you can find instructions here.
Meta strings
Can add strings to the top fence of a code block to add a title, a caption, highlight lines and/or characters, and show line number.
For example, this markdown…
```js title="constants/fruit.js" caption="Delish!" showLineNumbers {2-3,10} /orange/1-2 /banana/1
const fruit = [
'apple',
'apple',
'apple'
'orange',
'orange',
'orange',
'banana',
'banana',
'banana',
]
```
…will output this:
const fruit = [
'apple',
'apple',
'apple'
'orange',
'orange',
'orange',
'banana',
'banana',
'banana',
]
Or here’s a diff:
import useSWR from 'swr'
function Profile() {
const { data, error } = useSWR('/api/user', fetcher)
- if (error) return <div>failed to load</div>
+ if (!data) return <div>loading...</div>
return <div>hello {data.name}!</div>
}
Styling
The styling shown above is all customizable by setting a theme and then adding styles to the following selectors (which are unstyled by default):
- TODO: add the rest
- line:
[data-line]
title
:[data-rehype-pretty-code-title]
- highlighted line:
[data-line-highlighted]
- highlighted characters:
[data-highlighted-chars]
Inline syntax highlighting
- This is what makes this library special
- Can also create shortcuts for custom inline highlighting tokens (show config setting and an example)
Multiple themes
- Note: to display markdown code blocks as markdown (instead of parsing them), wrap the code block in a `````markdown` fence with four backticks.
Further reading
Inbox
- Rehype Pretty Code • rehype plugin to add syntax highlighting to code blocks
- Build-Time Syntax Highlighting: Zero Client-Side JS, Support for 100+ Languages and Any VSCode Theme | delba.dev
- rehype-pretty-code/website/src/app/index.mdx at master · atomiks/rehype-pretty-code • Example of the Markdown (MDX) used to create the rehype-pretty-code homepage