Hugo has its own syntax highlighting using Chroma, but I wanted to keep things consistent with typical AsciiDoc. The use of AsciiDoc markup in Hugo depends on AsciiDoctor, which supports a number of syntax highlighters:
-
highlight.js
-
CodeRay
-
Pygments
-
Rouge
All but highlight.js are build-time highlighters, which apply the highlighting when the site is generated. I chose Rouge because it supports a large number of languages, unlike CodeRay, and does not require Python, unlike Pygments.
There are two ways to implement Rouge:
-
Using a Rouge theme
-
Using a CSS file
In either case, you need to first install Rouge:
-
Open a terminal in the root folder of your Hugo project and run the command
gem install rouge
. -
In the root folder, open the
gemfile
and add an entry for Rouge:gemfile1 2 3
source 'https://rubygems.org' gem 'asciidoctor' gem 'rouge'
-
Update the
gemfile.lock
file by runningbundle
. The file should then look something like this:gemfile.lockGEM remote: https://rubygems.org/ specs: asciidoctor (2.0.17) rouge (3.28.0) PLATFORMS x64-mingw32 DEPENDENCIES asciidoctor rouge BUNDLED WITH 2.2.33
Using a Rouge theme
This is the simpler approach:
-
Add the following entry to the
/<root-folder>/config/_default/config.toml
file:[markup.asciidocExt] [markup.asciidocExt.attributes] source-highlighter = "rouge" (1) rouge-style = "base16.solarized" (2) rouge-css = "style" (3)
-
Activates the Rouge highlighter. This is equivalent to adding
:source-highlighter: rouge
to the top of each AsciiDoc file that you create in your Hugo content folders. -
This is the name of the theme you want to use. Use the Rouge Theme Preview Page to choose a style.
-
This setting tells Rouge to use the specified theme. The default value is
class
.
-
Using CSS
This approach is a little more complicated, but it allows for tweaking the CSS to suit your site:
-
Download the Rouge theme that you want to use.
-
Run
rougify style yourstyle > yourstyle.css
to output the CSS to a text file. -
If your Hugo theme uses SCSS, use CSS to SCSS Converter to convert the file.
-
Do one of the following:
-
Either:
-
Add the
.css
or.scss
file to the relevant templates folder in your Hugo theme. For example, I added my file to<root-folder>/themes/airspace-hugo/assets/scss/templates/
-
Add an
@import
rule to the end of yourstyle.scss
file. For example, I added@import 'github.scss'
to the end of<root-folder>/themes/airspace-hugo/assets/scss/style.scss
-
-
Or:
-
Place the CSS into the default
custom.scss
(orcustom.css
) file below the top-levelassets
folder. In my case this was/<root-folder>/assets/scss/ustom.scss
.The styles should now be included automatically.
-
-
-
Add the following entry to the
/<root-folder>/config/_default/config.toml
file:[markup.asciidocExt] [markup.asciidocExt.attributes] source-highlighter = "rouge" rouge-css = "class" (1)
-
This line can be left out because
class
is the default setting.
-
Enabling line highlighting
Enabling line highlighting involves adding two CSS styles (provided in the AsciiDoctor documentation):
-
Copy the following into the default
custom.scss
(orcustom.css
) file below the top-levelassets
folder. In my case this was/<root-folder>/assets/scss/custom.scss
.custom.scsspre.rouge .hll { background-color: #ffc; } pre.rouge .hll * { background-color: initial; }
Alternatively, if you added a CSS file for the Rouge syntax highlighting, add these styles to the end of that file.
Now you can highlight lines in the code:
[source%linenums,javascript,highlight=7..8]
----
function search(term)
{
var results = index.search(term);
var target = document.querySelector("main");
while (target.firstChild)
target.removeChild(target.firstChild);
----
This results in:
1
2
3
4
5
6
7
8
function search(term)
{
var results = index.search(term);
var target = document.querySelector("main");
while (target.firstChild)
target.removeChild(target.firstChild);