How to get syntax highlighting in HTML generated by GitHub

January 24, 2021

GitHub has its own markdown renderer. Maybe you're using a package like Caleb Porzio's GitDown, which internally uses the GitHub API to parse markdown. Or maybe you're using the GitHub API to get the contents of an issue.

In any case, you're letting GitHub render the markdown.

GitHub's markdown renderer adds classes inside code blocks, which are used for syntax highlighting (making some parts of the code a different color than others). However, you need the right CSS file to make those different classes different colors.

Per the github-syntax-theme-generator repository, you've got two options. Pick one of these two CSS files, copy and paste the line into your HTML's <head>, and it'll syntax highlight the markdown outputted by GitHub's API.

<!-- for light background -->
<link rel="stylesheet" href="[email protected]/lib/github-light.css" />

<!-- for dark background -->
<link rel="stylesheet" href="[email protected]/lib/github-dark.css" />

Follow my twitter (@benborgers) to see the work-in-progress of coding projects Iā€™m working on!