Code Syntax Highlighting

Code Syntax Highlighting

If you have a site which has code snippets, you probably want to implement code syntax highlighting. How do you implement syntax highlighting? You can easily implement a solution in a few minutes using hightlight.js.

Within your HTML page(s) head tag, you will need to add the below three lines of code. The three lines of code import the required css, import the required JavaScript functions and initializes the JavaScript object.
<!-- Syntax Highlighting -->
<link href='//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/default.min.css' rel='stylesheet'/>
<script src='//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js'/>
<script>hljs.initHighlightingOnLoad();</script>
If you are using a tool like Blogger or WordPress you will need to add the three lines of code to your template. In Blogger it should look something like this:

After you implement the previous three lines of code, you just need to surround your code snippet with a pre and code tag. You can change the class attribute of the code tag to match the language you require, like cpp, css, etc. Class names are slightly different than language names so check here for the names to use in the class attribute. Also see below for which languages are available from CDN vs manual implementation.

You now can highlight html code by using the following as an example:
<pre><code class="html">
<body>Hello World</body>
</code></pre>
The CDN only supports the following languages:
  • Apache
  • Bash
  • C#
  • C++
  • CSS
  • CoffeeScript
  • Diff
  • HTML, XML
  • HTTP
  • Ini, TOML
  • JSON
  • Java
  • JavaScript
  • Makefile
  • Markdown
  • Nginx
  • Objective-C
  • PHP
  • Perl
  • Properties
  • Python
  • Ruby
  • SQL
  • Shell Session
I recommend sticking to the supported CDN languages but the below languages can be supported:
  • 1C:Enterprise (v7, v8)
  • ARM Assembly
  • AVR Assembler
  • Access log
  • ActionScript
  • Ada
  • AngelScript
  • AppleScript
  • ArcGIS Arcade
  • Arduino
  • AsciiDoc
  • AspectJ
  • Augmented Backus-Naur Form
  • AutoHotkey
  • AutoIt
  • Awk
  • Axapta
  • Backus–Naur Form
  • Basic
  • Brainfuck
  • C/AL
  • CMake
  • CSP
  • Caché Object Script
  • Cap’n Proto
  • Ceylon
  • Clean
  • Clojure
  • Clojure REPL
  • Coq
  • Crystal
  • D
  • DNS Zone file
  • DOS .bat
  • Dart
  • Delphi
  • Device Tree
  • Django
  • Dockerfile
  • Dust
  • ERB (Embedded Ruby)
  • Elixir
  • Elm
  • Erlang
  • Erlang REPL
  • Excel
  • Extended Backus-Naur Form
  • F#
  • FIX
  • Flix
  • Fortran
  • G-code (ISO 6983)
  • GAMS
  • GAUSS
  • GLSL
  • GML
  • Gherkin
  • Go
  • Golo
  • Gradle
  • Groovy
  • HSP
  • HTMLBars
  • Haml
  • Handlebars
  • Haskell
  • Haxe
  • Hy
  • IRPF90
  • ISBL
  • Inform 7
  • Intel x86 Assembly
  • Julia
  • Julia REPL
  • Kotlin
  • LDIF
  • LLVM IR
  • Lasso
  • Leaf
  • Less
  • Linden Scripting Language
  • Lisp
  • LiveCode
  • LiveScript
  • Lua
  • MEL
  • MIPS Assembly
  • Mathematica
  • Matlab
  • Maxima
  • Mercury
  • Microtik RouterOS script
  • Mizar
  • Mojolicious
  • Monkey
  • MoonScript
  • N1QL
  • NSIS
  • Nimrod
  • Nix
  • OCaml
  • OpenSCAD
  • Oracle Rules Language
  • Oxygene
  • Parser3
  • Pony
  • PostgreSQL SQL dialect and PL/pgSQL
  • PowerShell
  • Processing
  • Prolog
  • Protocol Buffers
  • Puppet
  • PureBASIC
  • Python profile
  • Q
  • QML
  • R
  • ReasonML
  • RenderMan RIB
  • RenderMan RSL
  • Roboconf
  • Rust
  • SAS
  • SCSS
  • SML
  • SQF
  • STEP Part 21
  • Scala
  • Scheme
  • Scilab
  • Smali
  • Smalltalk
  • Stan
  • Stata
  • Stylus
  • SubUnit
  • Swift
  • TP
  • Tagger Script
  • Tcl
  • TeX
  • Test Anything Protocol
  • Thrift
  • Twig
  • TypeScript
  • VB.NET
  • VBScript
  • VBScript in HTML
  • VHDL
  • Vala
  • Verilog
  • Vim Script
  • XL
  • XQuery
  • YAML
  • Zephir
  • crmsh
  • dsconfig
  • jboss-cli
  • pf
  • plaintext
You can also switch out the syntax highlighting style by changing the css used. You can see the available styles here. The only thing to note is you will have to add .min to the name. Here is an example of using the dark.css style:
<!--<link href='//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/default.min.css' rel='stylesheet'/>-->
<link href='//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/dark.min.css' rel='stylesheet'/>

Conclusion

The true story of this implementation is that it took me longer to write this article than it was to implement code syntax highlighting on my site. I do recommend that you implement this if you are displaying any code on your site. See my powershell-unzip-file post to see what things look like without syntax highlighting. I will say it for you, without code syntax highlighting the page is harder to follow and aesthetically displeasing.

Comments

Popular posts from this blog

Azure DevOps - Auto Approve PR with PAT

Ubuntu 18.04 - Install Android Emulator

Ansible Module - VMWare Update Guest PCI Device