highlight.js
Highlight.js (22.2k β) is an open-source library to syntax highlight code snippets on web pages. Prism is the main alternative.
- πΈ Free and Open-Source
- π Supports many languages (192+)
- π¨ Supports many themes (496+)
It's the library used by the popular Stack Overflow and Discord.
Load the library π
You can find the tag to load it on the cdnjs website. You should add tags for all languages you want to use, which is more secure than letting the script automatically import them β οΈ.
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/X.Y.Z/highlight.min.js" [...]></script>
Write Code π€
<pre><code class="language-c"
>int main() {
exit 0;
}
</code></pre>
Highlight Code π
Once the content to highlight is rendered, you need to call hljs
:
<script>
hljs.highlightAll();
</script>