transform.Highlight
Syntax
transform.Highlight INPUT LANG [OPTIONS]
Returns
template.HTML
Alias
highlight
The highlight
function uses the Chroma syntax highlighter, supporting over 200 languages with more than 40 available styles.
Arguments
- INPUT
- The code to highlight.
- LANG
- The language of the code to highlight. Choose from one of the supported languages. Case-insensitive.
- OPTIONS
- A map or comma-separated list of zero or more options. Set default values in site configuration.
Options
- anchorLineNos
- (
bool
) Whether to render each line number as an HTML anchor element, setting theid
attribute of the surroundingspan
element to the line number. Irrelevant iflineNos
isfalse
. Default isfalse
. - codeFences
- (
bool
) Whether to highlight fenced code blocks. Default istrue
. - guessSyntax
- (
bool
) Whether to automatically detect the language if theLANG
argument is blank or set to a language for which there is no corresponding lexer. Falls back to a plain text lexer if unable to automatically detect the language. Default isfalse
.
- hl_Lines
- (
string
) A space-delimited list of lines to emphasize within the highlighted code. To emphasize lines 2, 3, 4, and 7, set this value to2-4 7
. This option is independent of thelineNoStart
option. - hl_inline
- (
bool
) Whether to render the highlighted code without a wrapping container.Default isfalse
. - lineAnchors
- (
string
) When rendering a line number as an HTML anchor element, prepend this value to theid
attribute of the surroundingspan
element. This provides uniqueid
attributes when a page contains two or more code blocks. Irrelevant iflineNos
oranchorLineNos
isfalse
. - lineNoStart
- (
int
) The number to display at the beginning of the first line. Irrelevant iflineNos
isfalse
. Default is1
. - lineNos
- (
bool
) Whether to display a number at the beginning of each line. Default isfalse
. - lineNumbersInTable
- (
bool
) Whether to render the highlighted code in an HTML table with two cells. The left table cell contains the line numbers, while the right table cell contains the code. Irrelevant iflineNos
isfalse
. Default istrue
. - noClasses
- (
bool
) Whether to use inline CSS styles instead of an external CSS file. To use an external CSS file, set this value tofalse
and generate the CSS file using thehugo gen chromastyles
command. Default istrue
. - style
- (
string
) The CSS styles to apply to the highlighted code. See the style gallery for examples. Case-sensitive. Default ismonokai
. - tabWidth
- (
int
) Substitute this number of spaces for each tab character in your highlighted code. Irrelevant ifnoClasses
isfalse
. Default is4
. - wrapperClass
- New in v0.140.2
- (
string
) The class or classes to use for the outermost element of the highlighted code. Default ishighlight
.
Examples
{{ $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" }}
{{ $opts := dict "lineNos" "table" "style" "dracula" }}
{{ transform.Highlight $input $lang $opts }}