I struggled to get the code blocks in my blog looking exactly how I wanted when creating this site, so I decided to share exactly how I went about styling them.
firstly you must install next/mdx, rehype-pretty-code, and a variety of dependencies:
Once everything has been installed we can begin to write the code that reads our MDX files (Markdown with JSX elements) and applies our styling to the code blocks.
In this example, I am retrieving the markdown files from a local directory using the file system in node.
Once we have received our posts we will use the MDXRemote element to render the markdown into html.
Now that we can render our markdown files into HTML on the site, we need to configure it to run through rehype-pretty-code. We do this by passing it into the MDXRemote element's option prop. Additionally we are setting the theme we want using shiki - a list of packaged themes can be found here.
Now we can add custom styling to the code blocks as detailed in the rehype-prety-code documentation. below are some examples of what I did.
The following CSS snippet shows the required theming to get line numbers to show in your code boxes:
Hopefully, this helps you to get the ball running on your own simple markdown blog in NextJS!