highlight.js
September 7 201796 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
- 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>
- In the
<code>section, add appropriateCSSclass 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.