hugo/content/en/content-management/syntax-highlighting.md
Bjørn Erik Pedersen 9e1dcefc5f Squashed 'docs/' changes from 6c2195936..9be494de3
9be494de3 Clean up the markup config
c3e123133 Remove JustComment
bc1b02bc5 Add Smart to Anchor section
49e796409 Added where .RegularPagesRecursive was added
a92296e31 Fixed the new-in shortcode in pages-vs-site-pages
051e1267c Documented .RegularPagesRecursive
5bcec88a3 Fix broken link in RSS templates page
3db79d433 Correct pull request link on migrations.md
55c347168 Release 0.71.1
11a4e36c3 Merge branch 'temp711'
481fc8ed6 releaser: Add release notes to /docs for release of 0.71.1
40ba83c26 Update site.md
382632b58 Use-modules: More help how to get started importing a theme (#1107)
06751d465 Addition of hugefastsearch to search options (#1118)
8346d3b18 Add example of how to set the permalinks option for pages in "root"
ebb3b4f3a Refine highlight shortcode options
1075a172a Update index.md
7cc927ea6 Fix typo in v0.71.0 release notes
4121da273 Pull in latest theme version to get link hooks
4809be651 Document render-heading feature
2078a3bd1 Release 0.71.0
c09f6899e releaser: Add release notes to /docs for release of 0.71.0
90ffe2b50 Merge commit 'c9403cbceaaeff53ff4833561f4eefe1dc1a405e'
bf3dd0837 Add math.Pow

git-subtree-dir: docs
git-subtree-split: 9be494de3ac79081be60e0f002db110cb96ec7a3
2020-05-31 12:43:23 +02:00

4.6 KiB

title description date publishdate keywords categories menu weight sections_weight draft aliases toc
Syntax Highlighting Hugo comes with really fast syntax highlighting from Chroma. 2017-02-01 2017-02-01
highlighting
chroma
code blocks
syntax
content management
docs
parent weight
content-management 300
20 20 false
/extras/highlighting/
/extras/highlight/
/tools/syntax-highlighting/
true

Hugo uses Chroma as its code highlighter; it is built in Go and is really, really fast -- and for the most important parts compatible with Pygments we used before.

Configure Syntax Highlighter

See Configure Highlight.

Generate Syntax Highlighter CSS

If you run with pygmentsUseClasses=true in your site config, you need a style sheet.

You can generate one with Hugo:

hugo gen chromastyles --style=monokai > syntax.css

Run hugo gen chromastyles -h for more options. See https://xyproto.github.io/splash/docs/ for a gallery of available styles.

Highlight Shortcode

Highlighting is carried out via the built-in shortcode highlight. highlight takes exactly one required parameter for the programming language to be highlighted and requires a closing shortcode. Note that highlight is not used for client-side javascript highlighting.

Options:

  • linenos: configure line numbers. Valid values are true, false, table, or inline. false will turn off line numbers if it's configured to be on in site config. {{< new-in "0.60.0" >}} table will give copy-and-paste friendly code blocks.
  • hl_lines: lists a set of line numbers or line number ranges to be highlighted.
  • linenostart=199: starts the line number count from 199.

Example: Highlight Shortcode

{{</* highlight go "linenos=table,hl_lines=8 15-17,linenostart=199" */>}}
// ... code
{{</* / highlight */>}}

Gives this:

{{< highlight go "linenos=table,hl_lines=8 15-17,linenostart=199" >}} // GetTitleFunc returns a func that can be used to transform a string to // title case. // // The supported styles are // // - "Go" (strings.Title) // - "AP" (see https://www.apstylebook.com/) // - "Chicago" (see https://www.chicagomanualofstyle.org/home.html) // // If an unknown or empty style is provided, AP style is what you get. func GetTitleFunc(style string) func(s string) string { switch strings.ToLower(style) { case "go": return strings.Title case "chicago": return transform.NewTitleConverter(transform.ChicagoStyle) default: return transform.NewTitleConverter(transform.APStyle) } } {{< / highlight >}}

Highlight Template Func

See Highlight.

Highlighting in Code Fences

Highlighting in code fences is enabled by default.{{< new-in "0.60.0" >}}

```go {linenos=table,hl_lines=[8,"15-17"],linenostart=199}
// ... code
```

Gives this:

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 // GetTitleFunc returns a func that can be used to transform a string to // title case. // // The supported styles are // // - "Go" (strings.Title) // - "AP" (see https://www.apstylebook.com/) // - "Chicago" (see https://www.chicagomanualofstyle.org/home.html) // // If an unknown or empty style is provided, AP style is what you get. func GetTitleFunc(style string) func(s string) string { switch strings.ToLower(style) { case "go": return strings.Title case "chicago": return transform.NewTitleConverter(transform.ChicagoStyle) default: return transform.NewTitleConverter(transform.APStyle) } }

{{< new-in "0.60.0" >}}Note that only Goldmark supports passing attributes such as hl_lines, and it's important that it does not contain any spaces. See goldmark-highlighting for more information.

The options are the same as in the highlighting shortcode,including linenos=false, but note the slightly different Markdown attribute syntax.

List of Chroma Highlighting Languages

The full list of Chroma lexers and their aliases (which is the identifier used in the highlight template func or when doing highlighting in code fences):

{{< chroma-lexers >}}