2019-10-21 04:22:28 -04:00
---
2023-07-29 05:15:54 -04:00
title: Syntax highlighting
2019-10-21 04:22:28 -04:00
description: Hugo comes with really fast syntax highlighting from Chroma.
categories: [content management]
2023-12-04 09:14:18 -05:00
keywords: [highlighting,chroma,code blocks,syntax]
2019-10-21 04:22:28 -04:00
menu:
docs:
2022-11-17 10:14:29 -05:00
parent: content-management
weight: 240
weight: 240
2023-12-04 09:14:18 -05:00
toc: true
2022-11-17 10:14:29 -05:00
aliases: [/extras/highlighting/,/extras/highlight/,/tools/syntax-highlighting/]
2019-10-21 04:22:28 -04:00
---
2022-11-17 10:14:29 -05:00
Hugo uses [Chroma ](https://github.com/alecthomas/chroma ) as its code highlighter; it is built in Go and is really, really fast.
2019-10-21 04:22:28 -04:00
2023-07-29 05:15:54 -04:00
## Configure syntax highlighter
2019-12-15 04:35:09 -05:00
See [Configure Highlight ](/getting-started/configuration-markup#highlight ).
2023-07-29 05:15:54 -04:00
## Generate syntax highlighter CSS
2019-12-15 04:35:09 -05:00
2023-07-29 05:15:54 -04:00
If you run with `markup.highlight.noClasses=false` in your site configuration, you need a style sheet.
2019-12-15 04:35:09 -05:00
You can generate one with Hugo:
2019-10-21 04:22:28 -04:00
2023-12-04 09:14:18 -05:00
```sh
2019-12-15 04:35:09 -05:00
hugo gen chromastyles --style=monokai > syntax.css
```
Run `hugo gen chromastyles -h` for more options. See https://xyproto.github.io/splash/docs/ for a gallery of available styles.
2023-07-29 05:15:54 -04:00
## Highlight shortcode
2019-12-15 04:35:09 -05:00
2023-07-29 05:15:54 -04:00
Highlighting is carried out via the built-in [`highlight` shortcode ](/content-management/shortcodes/#highlight ). It takes exactly one required parameter for the programming language to be highlighted and requires a closing shortcode.
2019-12-15 04:35:09 -05:00
Options:
2023-07-29 05:15:54 -04:00
* `linenos` : configure line numbers. Valid values are `true` , `false` , `table` , or `inline` . `false` will turn off line numbers if it's configured to be on in site configuration. `table` will give copy-and-paste friendly code blocks.
2020-05-31 06:43:23 -04:00
* `hl_lines` : lists a set of line numbers or line number ranges to be highlighted.
* `linenostart=199` : starts the line number count from 199.
2020-10-06 10:22:20 -04:00
* `anchorlinenos` : Configure anchors on line numbers. Valid values are `true` or `false` ;
* `lineanchors` : Configure a prefix for the anchors on line numbers. Will be suffixed with `-` , so linking to the line number 1 with the option `lineanchors=prefix` adds the anchor `prefix-1` to the page.
2023-12-04 09:14:18 -05:00
* `hl_inline` Highlight inside a `<code>` (inline HTML element) tag. Valid values are `true` or `false` . The `code` tag will get a class with name `code-inline` . {{< new-in 0 . 101 . 0 > }}
2019-10-21 04:22:28 -04:00
2023-07-29 05:15:54 -04:00
### Example: highlight shortcode
2019-10-21 04:22:28 -04:00
2022-11-17 10:14:29 -05:00
```go-html-template
2019-10-21 04:22:28 -04:00
{{< /* highlight go "linenos=table,hl_lines=8 15-17,linenostart=199" */>}}
// ... code
{{< /* / highlight */>}}
```
Gives this:
{{< highlight go " linenos = table,hl_lines=8 15-17 , linenostart = 199" > }}
// GetTitleFunc returns a func that can be used to transform a string to
// title case.
//
// The supported styles are
//
// - "Go" (strings.Title)
// - "AP" (see https://www.apstylebook.com/)
// - "Chicago" (see https://www.chicagomanualofstyle.org/home.html)
//
// If an unknown or empty style is provided, AP style is what you get.
func GetTitleFunc(style string) func(s string) string {
switch strings.ToLower(style) {
case "go":
return strings.Title
case "chicago":
2019-12-15 04:35:09 -05:00
return transform.NewTitleConverter(transform.ChicagoStyle)
2019-10-21 04:22:28 -04:00
default:
2019-12-15 04:35:09 -05:00
return transform.NewTitleConverter(transform.APStyle)
2019-10-21 04:22:28 -04:00
}
}
{{< / highlight > }}
2023-07-29 05:15:54 -04:00
## Highlight Hugo/Go template code
2022-09-13 14:34:24 -04:00
2023-07-29 05:15:54 -04:00
For highlighting Hugo/Go template code on your page, add `/*` after the opening double curly braces and `*/` before closing curly braces.
2022-09-13 14:34:24 -04:00
``` go
{{< /*/* myshortcode */* />}}
```
Gives this:
``` go
{{< /* myshortcode */>}}
```
2023-07-29 05:15:54 -04:00
## Highlight template function
2019-10-21 04:22:28 -04:00
2023-10-20 03:42:39 -04:00
See [Highlight ](/functions/transform/highlight/ ).
2019-10-21 04:22:28 -04:00
2023-07-29 05:15:54 -04:00
## Highlighting in code fences
2019-10-21 04:22:28 -04:00
2022-11-17 10:14:29 -05:00
Highlighting in code fences is enabled by default.
2019-10-21 04:22:28 -04:00
2022-11-17 10:14:29 -05:00
````txt
2019-12-15 04:35:09 -05:00
```go {linenos=table,hl_lines=[8,"15-17"],linenostart=199}
// ... code
2019-10-21 04:22:28 -04:00
```
````
2019-12-15 04:35:09 -05:00
Gives this:
2019-10-21 04:22:28 -04:00
2019-12-15 04:35:09 -05:00
```go {linenos=table,hl_lines=[8,"15-17"],linenostart=199}
// GetTitleFunc returns a func that can be used to transform a string to
// title case.
//
// The supported styles are
//
// - "Go" (strings.Title)
// - "AP" (see https://www.apstylebook.com/)
// - "Chicago" (see https://www.chicagomanualofstyle.org/home.html)
//
// If an unknown or empty style is provided, AP style is what you get.
func GetTitleFunc(style string) func(s string) string {
switch strings.ToLower(style) {
case "go":
return strings.Title
case "chicago":
return transform.NewTitleConverter(transform.ChicagoStyle)
default:
return transform.NewTitleConverter(transform.APStyle)
}
}
```
2019-10-21 04:22:28 -04:00
2023-05-22 10:43:12 -04:00
The options are the same as in the [highlighting shortcode ](/content-management/syntax-highlighting/#highlight-shortcode ), including `linenos=false` , but note the slightly different Markdown attribute syntax.
2019-10-21 04:22:28 -04:00
2023-07-29 05:15:54 -04:00
## List of Chroma highlighting languages
2019-10-21 04:22:28 -04:00
2019-12-15 04:35:09 -05:00
The full list of Chroma lexers and their aliases (which is the identifier used in the `highlight` template func or when doing highlighting in code fences):
2019-10-21 04:22:28 -04:00
2019-12-15 04:35:09 -05:00
{{< chroma-lexers > }}