2019-10-21 04:22:28 -04:00
---
title: highlight
2021-12-13 15:04:12 -05:00
description: Renders code with a syntax highlighter.
2019-10-21 04:22:28 -04:00
categories: [functions]
menu:
docs:
2023-05-22 10:43:12 -04:00
parent: functions
2019-12-15 04:35:09 -05:00
keywords: [highlighting,code blocks,syntax]
2021-12-13 15:04:12 -05:00
signature: ["transform.Highlight INPUT LANG [OPTIONS]","highlight INPUT LANG [OPTIONS]"]
2019-10-21 04:22:28 -04:00
relatedfuncs: []
2021-12-13 15:04:12 -05:00
toc: true
2019-10-21 04:22:28 -04:00
---
2021-12-13 15:04:12 -05:00
The `highlight` function uses the [Chroma] syntax highlighter, supporting over 200 languages with more than 40 available styles.
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
## Arguments
2019-10-21 04:22:28 -04:00
2021-12-13 15:04:12 -05:00
INPUT
: The code to highlight.
2019-10-21 04:22:28 -04:00
2021-12-13 15:04:12 -05:00
LANG
: The language of the code to highlight. Choose from one of the [supported languages]. Case-insensitive.
OPTIONS
: An optional, comma-separated list of zero or more [options]. Set default values in [site configuration].
## Options
lineNos
: Boolean. Default is `false` .\
Display a number at the beginning of each line.
lineNumbersInTable
: Boolean. Default is `true` .\
2023-02-23 01:52:04 -05:00
Render the highlighted code in an HTML table with two cells. The left table cell contains the line numbers. The right table cell contains the code, allowing a user to select and copy the code without line numbers. Irrelevant if `lineNos` is `false` .
2021-12-13 15:04:12 -05:00
anchorLineNos
: Boolean. Default is `false` .\
2023-02-23 01:52:04 -05:00
Render each line number as an HTML anchor element, and set the `id` attribute of the surrounding `<span>` to the line number. Irrelevant if `lineNos` is `false` .
2021-12-13 15:04:12 -05:00
lineAnchors
: String. Default is `""` .\
2023-02-23 01:52:04 -05:00
When rendering a line number as an HTML anchor element, prepend this value to the `id` attribute of the surrounding `<span>` . This provides unique `id` attributes when a page contains two or more code blocks. Irrelevant if `lineNos` or `anchorLineNos` is `false` .
2021-12-13 15:04:12 -05:00
lineNoStart
: Integer. Default is `1` .\
2023-02-23 01:52:04 -05:00
The number to display at the beginning of the first line. Irrelevant if `lineNos` is `false` .
2021-12-13 15:04:12 -05:00
hl_Lines
: String. Default is `""` .\
A space-separated list of lines to emphasize within the highlighted code. To emphasize lines 2, 3, 4, and 7, set this value to `2-4 7` . This option is independent of the `lineNoStart` option.
2022-06-28 14:51:33 -04:00
hl_inline
: Boolean. Default is `false` .\
Render the highlighted code without a wrapping container.
2021-12-13 15:04:12 -05:00
style
: String. Default is `monokai` .\
The CSS styles to apply to the highlighted code. See the [style gallery] for examples. Case-sensitive.
noClasses
: Boolean. Default is `true` .\
Use inline CSS styles instead of an external CSS file. To use an external CSS file, set this value to `false` and [generate the file with the hugo client][hugo client].
tabWidth
: Integer. Default is `4` .\
2023-02-23 01:52:04 -05:00
Substitute this number of spaces for each tab character in your highlighted code. Irrelevant if `noClasses` is `false` .
2021-12-13 15:04:12 -05:00
guessSyntax
: Boolean. Default is `false` .\
2023-08-30 13:23:47 -04:00
If the `LANG` argument is blank or an unrecognized language, auto-detect the language if possible, otherwise use a fallback language.
2021-12-13 15:04:12 -05:00
{{% note %}}
Instead of specifying both `lineNos` and `lineNumbersInTable` , you can use the following shorthand notation:
2022-11-17 10:14:29 -05:00
`lineNos=inline`
2021-12-13 15:04:12 -05:00
: equivalent to `lineNos=true` and `lineNumbersInTable=false`
2022-11-17 10:14:29 -05:00
`lineNos=table`
2021-12-13 15:04:12 -05:00
: equivalent to `lineNos=true` and `lineNumbersInTable=true`
{{% /note %}}
## Examples
```go-html-template
{{ $input := `fmt.Println("Hello World!")` }}
{{ transform.Highlight $input "go" }}
{{ $input := `console.log('Hello World!');` }}
{{ $lang := "js" }}
{{ transform.Highlight $input $lang "lineNos=table, style=api" }}
{{ $input := `echo "Hello World!"` }}
{{ $lang := "bash" }}
2023-07-29 05:15:54 -04:00
{{ $options := dict "lineNos" "table" "style" "dracula" }}
{{ transform.Highlight $input $lang $options }}
2021-12-13 15:04:12 -05:00
```
[Chroma]: https://github.com/alecthomas/chroma
2023-05-22 10:43:12 -04:00
[hugo client]: /commands/hugo_gen_chromastyles
[options]: #options
[site configuration]: /getting-started/configuration-markup#highlight
2021-12-13 15:04:12 -05:00
[style gallery]: https://xyproto.github.io/splash/docs/
2023-05-22 10:43:12 -04:00
[supported languages]: /content-management/syntax-highlighting#list-of-chroma-highlighting-languages