If you want to show example code on a page in Nextjs you will help your readers by implemeting some form of syntax highlighting. Here's how.
Date:
Words: 274
Time: 1 min read
Adding highlighting in a markdown-based blog
1
Add syntax highlights to markdown code
If you are using something like Remark you can simply add remark-prism to your markdown processing as follows.
2
Add css
Then, make sure you load a prism css theme to actually colorize your code.
Adding highlighting to HTML directly
In this case we will be moving some of the work from the server to the client
1
Install prism js using npm / yarn
Prismjs also provides TypeScript support, npm i @types/prismjs or yarn add @types/prismjs
1
Use prismjs on your page
Import prismjs into your blog page and highlight all.
The next step is just import prismjs into your codes and call the highlightAll function in your useEffect / componentDidMount.
It will automatically select the code blocks and will highlight them all. Note, you must have your code in the format above.