| 1 |
--- |
| 2 |
title: transform.Highlight |
| 3 |
description: Renders code with a syntax highlighter. |
| 4 |
categories: [] |
| 5 |
keywords: [highlight] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [highlight] |
| 9 |
returnType: template.HTML |
| 10 |
signatures: ['transform.Highlight CODE [LANG] [OPTIONS]'] |
| 11 |
aliases: [/functions/highlight] |
| 12 |
--- |
| 13 |
|
| 14 |
The `transform.Highlight` function uses the [`alecthomas/chroma`][] package to generate syntax-highlighted HTML from the provided code, [language][], and [options](#options-1). |
| 15 |
|
| 16 |
## Arguments |
| 17 |
|
| 18 |
`CODE` |
| 19 |
: (`string`) The code to highlight. |
| 20 |
|
| 21 |
`LANG` |
| 22 |
: (`string`) The [language][] of the code to highlight. This value is case-insensitive. Optional; you can also set the language with the `type` key in OPTIONS. {{< new-in 0.162.0 />}} |
| 23 |
|
| 24 |
`OPTIONS` |
| 25 |
: (`map or string`) A map or comma-separated key-value pairs wrapped in quotation marks. See the [options](#options-1) below; you can set default values for each option in your [project configuration][]. The key names are case-insensitive. |
| 26 |
|
| 27 |
## Examples |
| 28 |
|
| 29 |
```go-html-template |
| 30 |
{{ $input := `fmt.Println("Hello World!")` }} |
| 31 |
{{ transform.Highlight $input "go" }} |
| 32 |
|
| 33 |
{{ $input := `console.log('Hello World!');` }} |
| 34 |
{{ $lang := "js" }} |
| 35 |
{{ transform.Highlight $input $lang "lineNos=table, style=api" }} |
| 36 |
|
| 37 |
{{ $input := `echo "Hello World!"` }} |
| 38 |
{{ $lang := "bash" }} |
| 39 |
{{ $opts := dict "lineNos" "table" "style" "dracula" }} |
| 40 |
{{ transform.Highlight $input $lang $opts }} |
| 41 |
|
| 42 |
{{ $input := `print("Hello World!")` }} |
| 43 |
{{ $opts := dict "type" "python" "style" "dracula" }} |
| 44 |
{{ transform.Highlight $input $opts }} |
| 45 |
``` |
| 46 |
|
| 47 |
## Options |
| 48 |
|
| 49 |
The `transform.Highlight` function accepts an options map. |
| 50 |
|
| 51 |
{{% include "_common/syntax-highlighting-options.md" %}} |
| 52 |
|
| 53 |
`code` |
| 54 |
: {{< new-in 0.162.0 />}} |
| 55 |
: (`string`) Overrides the `CODE` argument. |
| 56 |
|
| 57 |
`type` |
| 58 |
: {{< new-in 0.162.0 />}} |
| 59 |
: (`string`) Overrides the `LANG` argument. |
| 60 |
|
| 61 |
[`alecthomas/chroma`]: https://github.com/alecthomas/chroma |
| 62 |
[language]: /content-management/syntax-highlighting#languages |
| 63 |
[project configuration]: /configuration/markup#highlight |