Highlight shortcode
To override Hugo’s embedded highlight shortcode, copy the source code to a file with the same name in the layouts/_shortcodes directory.
With the Markdown content format, the highlight shortcode is rarely needed because, by default, Hugo automatically applies syntax highlighting to fenced code blocks.
The primary use case for the highlight shortcode in Markdown is to apply syntax highlighting to inline code snippets.
The highlight shortcode calls the transform.Highlight function which uses the Chroma syntax highlighter, supporting over 200 languages with more than 40 highlighting styles.
Arguments
The highlight shortcode takes three arguments.
{{< highlight LANG OPTIONS >}}
CODE
{{< /highlight >}}- CODE
- (
string) The code to highlight. - LANG
- (
string) The language of the code to highlight. Choose from one of the supported languages. This value is case-insensitive. - OPTIONS
- (
string) Zero or more space-separated key-value pairs wrapped in quotation marks. Set default values for each option in your site configuration. The key names are case-insensitive.
Example
{{< highlight go "linenos=inline, hl_lines=3 6-8, style=emacs" >}}
package main
import "fmt"
func main() {
for i := 0; i < 3; i++ {
fmt.Println("Value of i:", i)
}
}
{{< /highlight >}}Hugo renders this to:
1package main
2
3import "fmt"
4
5func main() {
6 for i := 0; i < 3; i++ {
7 fmt.Println("Value of i:", i)
8 }
9}You can also use the highlight shortcode for inline code snippets:
This is some {{< highlight go "hl_inline=true" >}}fmt.Println("inline"){{< /highlight >}} code.Hugo renders this to:
This is some fmt.Println("inline") code.
Given the verbosity of the example above, if you need to frequently highlight inline code snippets, create your own shortcode using a shorter name with preset options.
{{ $code := .Inner | strings.TrimSpace }}
{{ $lang := or (.Get 0) "go" }}
{{ $opts := dict "hl_inline" true "noClasses" true }}
{{ transform.Highlight $code $lang $opts }}This is some {{< hl >}}fmt.Println("inline"){{< /hl >}} code.Hugo renders this to:
This is some fmt.Println("inline")
code.
Options
Pass the options when calling the shortcode. You can set their default values in your site configuration.
- 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 Chroma syntax highlighter includes lexers for approximately 250 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. Case-sensitive. 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.
