mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-14 20:37:55 -05:00
a6e635ca7d
fcc3ed651 Remove some expired new-in a9c5981f5 Fix cascade example 82bb250fa Add some lines about permalinks tokens in front matter 328fe564e Remove some outdated new-in fb140b153 Hide showcase menu entry 42d9d1c79 Update image formats from which EXIF data can be extracted 09ad56b6e netlify: Hugo 0.130.0 1d503f846 Merge branch 'tempv0.130.0' e2458074d math: Add trigonometric functions and some angle helper functions 392afc8f9 Disable the showcase section for now 0300750f2 Improve example of image render hook 60a9306af Improve description of the .Site.RegularPages method 8d759175d Fix typos 55daa4554 Update XxHash.md 397c81cb7 Add namespace for hash functions 70fe8d2f0 netlify: Bump Hugo 0.129.0 5a9771aff Merge branch 'tempv0.129.0' f9146575b Fix typo e6e1fea49 Fix typo in Hugo docs | functions | partial 732d10ec4 source: Expose GitInfo Body 34c97e639 netlify: Hugo 0.128.2 3270587e9 Fix typo 727c5396e netlify: Hugo 0.128.1 80b6ae99c Update GitHub Pages workflow file example 027134102 Update GitHub Pages workflow file example 2600a8a2e Miscellaneous edits 3fdd5819b Update Build.md 7764005c3 Improve example of render hook directory structure 5e3941d82 Fix typos 748bf065f Restructure templates section fafbf6566 Update Defer.md 012162e0d Document changes to template functions in v0.128.0 0990ce35b quick-reference: Update emojis 6677a30ef Update Goldmark configuration documentation 4449d530d Document new pagination config 0af8be439 Update Defer.md 56348196d netlify: Hugo 0.128.0 d67b6d82e Update content/en/functions/templates/Defer.md 23d996b3d Update content/en/functions/templates/Defer.md 7f7fb2f27 Document templates.Defer 5ada1e9d5 Fix docs merge (remove shortcode) d27ee6156 Merge branch 'tempv0.128.0' 5d7317c84 Fix typo 7c18ee546 Update theme 83bfea63b Update theme b274b3238 Merge commit '8b9803425e63e1b1801f8d5d676e96368d706722' ff34a035a deploy: Add stripIndexHtml target option d9e964bdb markup/goldmark: Add the Hugo Goldmark Extras "delete" extension ac5bd16d2 deps: Upgrade github.com/alecthomas/chroma v2.13.0 => v2.14.0 25377171b config: Remove extraneous BuildConfig setting 0d2044f6d docs: Regen docshelper a2548dac9 markup/goldmark: Support extras extension 9d0c86ee8 commands: Add gen chromastyles --lineNumbersTableStyle flag git-subtree-dir: docs git-subtree-split: fcc3ed651a1b6431303c2f88f20fa38531c52b3d
138 lines
4.6 KiB
Markdown
138 lines
4.6 KiB
Markdown
---
|
|
title: Syntax highlighting
|
|
description: Hugo comes with really fast syntax highlighting from Chroma.
|
|
categories: [content management]
|
|
keywords: [highlighting,chroma,code blocks,syntax]
|
|
menu:
|
|
docs:
|
|
parent: content-management
|
|
weight: 250
|
|
weight: 250
|
|
toc: true
|
|
aliases: [/extras/highlighting/,/extras/highlight/,/tools/syntax-highlighting/]
|
|
---
|
|
|
|
Hugo uses [Chroma](https://github.com/alecthomas/chroma) as its code highlighter; it is built in Go and is really, really fast.
|
|
|
|
## Configure syntax highlighter
|
|
|
|
See [Configure Highlight](/getting-started/configuration-markup#highlight).
|
|
|
|
## Generate syntax highlighter CSS
|
|
|
|
If you run with `markup.highlight.noClasses=false` in your site configuration, you need a style sheet. The style sheet will override the style specified in [`markup.highlight.style`](/functions/transform/highlight/#options).
|
|
|
|
You can generate one with Hugo:
|
|
|
|
```sh
|
|
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 [`highlight` shortcode](/content-management/shortcodes/#highlight). It takes exactly one required argument for the programming language to be highlighted and requires a closing tag.
|
|
|
|
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 configuration. `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.
|
|
* `anchorlinenos`: Configure anchors on line numbers. Valid values are `true` or `false`;
|
|
* `lineanchors`: Configure a prefix for the anchors on line numbers. Will be suffixed with `-`, so linking to the line number 1 with the option `lineanchors=prefix` adds the anchor `prefix-1` to the page.
|
|
* `hl_inline` Highlight inside a `<code>` (inline HTML element) tag. Valid values are `true` or `false`. The `code` tag will get a class with name `code-inline`.
|
|
|
|
### Example: highlight shortcode
|
|
|
|
```go-html-template
|
|
{{</* 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 Hugo/Go template code
|
|
|
|
For highlighting Hugo/Go template code on your page, add `/*` after the opening double curly braces and `*/` before closing curly braces.
|
|
|
|
``` go
|
|
{{</*/* myshortcode */*/>}}
|
|
```
|
|
|
|
Gives this:
|
|
|
|
``` go
|
|
{{</* myshortcode */>}}
|
|
```
|
|
|
|
## Highlight template function
|
|
|
|
See [Highlight](/functions/transform/highlight/).
|
|
|
|
## Highlighting in code fences
|
|
|
|
Highlighting in code fences is enabled by default.
|
|
|
|
````txt
|
|
```go {linenos=table,hl_lines=[8,"15-17"],linenostart=199}
|
|
// ... code
|
|
```
|
|
````
|
|
|
|
Gives this:
|
|
|
|
```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)
|
|
}
|
|
}
|
|
```
|
|
|
|
The options are the same as in the [highlighting shortcode](/content-management/syntax-highlighting/#highlight-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 >}}
|