Debian Codegraph @

docs/content/en/shortcodes/highlight.md

raw
1 ---
2 title: Highlight shortcode
3 linkTitle: Highlight
4 description: Insert syntax-highlighted code into your content using the highlight shortcode.
5 categories: []
6 keywords: [highlight]
7 ---
8
9 > [!NOTE]
10 > To override Hugo's embedded `highlight` shortcode, copy the [source code][] to a file with the same name in the `layouts/_shortcodes` directory.
11
12 > [!NOTE]
13 > With the Markdown [content format][], the `highlight` shortcode is rarely needed because, by default, Hugo automatically applies syntax highlighting to fenced code blocks.
14 >
15 > The primary use case for the `highlight` shortcode in Markdown is to apply syntax highlighting to inline code snippets.
16
17 The `highlight` shortcode calls the [`transform.Highlight`][] function to generate syntax-highlighted HTML from the provided code, [language][], and [options](#options-1).
18
19 ## Arguments
20
21 The `highlight` shortcode takes three arguments.
22
23 ```md
24 {{</* highlight LANG OPTIONS */>}}
25 CODE
26 {{</* /highlight */>}}
27 ```
28
29 `CODE`
30 : (`string`) The code to highlight.
31
32 `LANG`
33 : (`string`) The [language][] of the code to highlight. This value is case-insensitive.
34
35 `OPTIONS`
36 : (`string`) Zero or more space-separated key-value pairs wrapped in quotation marks. You can set default values for each option in your [project configuration][]. The key names are case-insensitive.
37
38 ## Example
39
40 ```md {file="content/example.md"}
41 {{</* highlight go "linenos=inline, hl_lines=3 6-8, style=emacs" */>}}
42 package main
43
44 import "fmt"
45
46 func main() {
47 for i := 0; i < 3; i++ {
48 fmt.Println("Value of i:", i)
49 }
50 }
51 {{</* /highlight */>}}
52 ```
53
54 Hugo renders this to:
55
56 {{< highlight go "linenos=inline, hl_Lines=3 6-8, noClasses=true" >}}
57 package main
58
59 import "fmt"
60
61 func main() {
62 for i := 0; i < 3; i++ {
63 fmt.Println("Value of i:", i)
64 }
65 }
66 {{< /highlight >}}
67
68 You can also use the `highlight` shortcode for inline code snippets:
69
70 ```md
71 This is some {{</* highlight go "hl_inline=true" */>}}fmt.Println("inline"){{</* /highlight */>}} code.
72 ```
73
74 Hugo renders this to:
75
76 This is some {{< highlight go "hl_inline=true, noClasses=true" >}}fmt.Println("inline"){{< /highlight >}} code.
77
78 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.
79
80 ```go-html-template {file="layouts/_shortcodes/hl.html"}
81 {{ $code := .Inner | strings.TrimSpace }}
82 {{ $lang := or (.Get 0) "go" }}
83 {{ $opts := dict "hl_inline" true "noClasses" true }}
84 {{ transform.Highlight $code $lang $opts }}
85 ```
86
87 ```md
88 This is some {{</* hl */>}}fmt.Println("inline"){{</* /hl */>}} code.
89 ```
90
91 Hugo renders this to:
92
93 This is some {{< hl >}}fmt.Println("inline"){{< /hl >}} code.
94
95 ## Options
96
97 {{% include "_common/syntax-highlighting-options.md" %}}
98
99 [`transform.Highlight`]: /functions/transform/highlight/
100 [content format]: /content-management/formats/
101 [language]: /content-management/syntax-highlighting/#languages
102 [project configuration]: /configuration/markup/#highlight
103 [source code]: <{{% eturl highlight %}}>