Bala's Blog

highlight.js

September 7 2017
96 words, ~1 min. read
javascript,  highlight 

highlight.js is a JavaScript library, which can be used to automatically detect language and highlight source code accordingly.

Usage

  1. Include the following inside the head section of the HTML,
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
  1. In the <code> section, add appropriate CSS class for the language you wish to highlight; for example,
<pre><code class="go">package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello")
}
</code></pre>

produces the following,

package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello")
}

Note : Since highlight.js does not support line numbering, in order to display source listings with line numbers, use highlightjs-line-numbers.