2019-10-21 04:22:28 -04:00
---
2023-10-20 03:42:39 -04:00
title: transform.Highlight
2021-12-13 15:04:12 -05:00
description: Renders code with a syntax highlighter.
2023-12-04 09:14:18 -05:00
categories: []
2023-10-20 03:42:39 -04:00
keywords: []
2023-12-04 09:14:18 -05:00
action:
2023-10-20 03:42:39 -04:00
aliases: [highlight]
2023-12-04 09:14:18 -05:00
related:
- functions/transform/CanHighlight
- functions/transform/HighlightCodeBlock
2023-10-20 03:42:39 -04:00
returnType: template.HTML
signatures: ['transform.Highlight INPUT LANG [OPTIONS]']
aliases: [/functions/highlight]
2021-12-13 15:04:12 -05:00
toc: true
2019-10-21 04:22:28 -04:00
---
2023-10-20 03:42:39 -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
2023-12-04 09:14:18 -05:00
: A map, or comma-separated list, of zero or more [options]. Set default values in [site configuration].
2021-12-13 15:04:12 -05:00
## 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 `""` .\
2023-12-04 09:14:18 -05:00
A space-delimited 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.
2021-12-13 15:04:12 -05:00
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:
2023-12-04 09:14:18 -05:00
lineNos=inline
2021-12-13 15:04:12 -05:00
: equivalent to `lineNos=true` and `lineNumbersInTable=false`
2023-12-04 09:14:18 -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-12-04 09:14:18 -05:00
{{ $opts := dict "lineNos" "table" "style" "dracula" }}
{{ transform.Highlight $input $lang $opts }}
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