While I was creating this blog's template, I wanted to convert Markdown files into HTML ones with code syntax highlighting.
Fortunately, there is a remark that works with markdown as structured data.
As dependencies, I need these npm packages:
For conversion, the code looks this. You see the highlighing works :)
One step further, as rehype-highlight mentioned, it's highly recommened to sanitaze markdown input so that we are not open up to XSS vulnerabilities.
I installed the sanitization system although I statically convert markdown files before deployment:
...and updated above code. rehype-highlight
uses highlight.js
inside and it automatically adds classes to highlight syntaxes. We need to allow them.
For styling, highlight.js has preset css files so we can just pick one and customize it if we want to. My favorite is github styles.