transform.Highlight
Renders code with a syntax highlighter.
Syntax
transform.Highlight CODE [LANG] [OPTIONS]
Returns
template.HTML
Alias
highlight
The transform.Highlight function uses the alecthomas/chroma package to generate syntax-highlighted HTML from the provided code, language, and options.
Arguments
CODE- (
string) The code to highlight. LANG- (
string) The language of the code to highlight. This value is case-insensitive. Optional; you can also set the language with thetypekey in OPTIONS. New in v0.162.0 OPTIONS- (
map or string) A map or comma-separated key-value pairs wrapped in quotation marks. See the options below; you can set default values for each option in your project configuration. The key names are case-insensitive.
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 }}
{{ $input := `print("Hello World!")` }}
{{ $opts := dict "type" "python" "style" "dracula" }}
{{ transform.Highlight $input $opts }}Options
anchorLineNos- (
bool) Whether to render each line number as an HTML anchor element, setting theidattribute of the surroundingspanelement to the line number. Irrelevant iflineNosisfalse. Default isfalse. codeFences- (
bool) Whether to highlight fenced code blocks. Default istrue. guessSyntax- (
bool) Whether to automatically detect the language if theLANGargument 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.The syntax highlighter includes lexers for approximately 300 languages, but only 5 of these have implemented automatic language detection.
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 thelineNoStartoption. 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 theidattribute of the surroundingspanelement. This provides uniqueidattributes when a page contains two or more code blocks. Irrelevant iflineNosoranchorLineNosisfalse. lineNoStart- (
int) The number to display at the beginning of the first line. Irrelevant iflineNosisfalse. Default is1. lineNos- (
any) Controls line number display. Default isfalse.true: Enable line numbers, controlled bylineNumbersInTable.false: Disable line numbers.inline: Enable inline line numbers (setslineNumbersInTabletofalse).table: Enable table-based line numbers (setslineNumbersInTabletotrue).
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 iflineNosisfalse. Default istrue. noClasses- (
bool) Whether to use inline CSS styles instead of an external CSS file. Default istrue. To use an external CSS file, set this value tofalseand generate the CSS file from the command line:hugo gen chromastyles --style=monokai > syntax.css style- (
string) The CSS styles to apply to the highlighted code. This value is case-insensitive. Default ismonokai. See syntax highlighting styles. tabWidth- (
int) Substitute this number of spaces for each tab character in your highlighted code. Irrelevant ifnoClassesisfalse. 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.
code- New in v0.162.0
- (
string) Overrides theCODEargument. type- New in v0.162.0
- (
string) Overrides theLANGargument.
