Debian Codegraph @

docs/content/en/content-management/syntax-highlighting.md

raw
1 ---
2 title: Syntax highlighting
3 description: Add syntax highlighting to code examples.
4 categories: []
5 keywords: [highlight]
6 aliases: [/extras/highlighting/,/extras/highlight/,/tools/syntax-highlighting/]
7 ---
8
9 Hugo provides several methods to add syntax highlighting to code examples:
10
11 - Use the [`transform.Highlight`][] function within your templates
12 - Use the [`highlight`][] shortcode with any [content format](g)
13 - Use fenced code blocks with the Markdown content format
14
15 ## Fenced code blocks
16
17 In its default configuration, Hugo highlights code examples within fenced code blocks, following this form:
18
19 ````md {file="content/example.md"}
20 ```LANG [OPTIONS]
21 CODE
22 ```
23 ````
24
25 `CODE`
26 : The code to highlight.
27
28 `LANG`
29 : The language of the code to highlight. Choose from one of the [supported languages](#languages). This value is case-insensitive. If omitted or unsupported, Hugo renders the text as a plain text block without syntax highlighting. Consistent with the [CommonMark][] specification, fenced code blocks require a known language identifier to trigger semantic syntax highlighting.
30
31 `OPTIONS`
32 : One or more space-separated or comma-separated key-value pairs wrapped in braces. Set default values for each option in your [project configuration][]. The key names are case-insensitive.
33
34 For example, with this Markdown:
35
36 ````md {file="content/example.md"}
37 ```go {linenos=inline hl_lines=[3,"6-8"] style=emacs}
38 package main
39
40 import "fmt"
41
42 func main() {
43 for i := 0; i < 3; i++ {
44 fmt.Println("Value of i:", i)
45 }
46 }
47 ```
48 ````
49
50 Hugo renders this:
51
52 ```go {linenos=inline, hl_lines=[3, "6-8"], style=emacs}
53 package main
54
55 import "fmt"
56
57 func main() {
58 for i := 0; i < 3; i++ {
59 fmt.Println("Value of i:", i)
60 }
61 }
62 ```
63
64 ## Options
65
66 {{% include "_common/syntax-highlighting-options.md" %}}
67
68 ## Escaping
69
70 When documenting shortcode usage, escape the tag delimiters:
71
72 ````md {file="content/example.md"}
73 ```text {linenos=inline}
74 {{</*/* shortcode-1 */*/>}}
75
76 {{%/*/* shortcode-2 */*/%}}
77 ```
78 ````
79
80 Hugo renders this to:
81
82 ```text {linenos=inline}
83 {{</* shortcode-1 */>}}
84
85 {{%/* shortcode-2 */%}}
86 ```
87
88 ## Languages
89
90 These are the supported languages. Use one of the identifiers, not the language name, when specifying a language for:
91
92 - The [`transform.Highlight`][] function
93 - The [`highlight`][] shortcode
94 - Fenced code blocks
95
96 {{< chroma-lexers >}}
97
98 [CommonMark]: https://spec.commonmark.org/current/#indented-code-blocks
99 [`highlight`]: /shortcodes/highlight/
100 [`transform.Highlight`]: /functions/transform/highlight/
101 [project configuration]: /configuration/markup/#highlight