mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-14 20:37:55 -05:00
2e711a28c7
af4b7ac5b Remove mention of Amber and Ace c841e9207 Exif docs corrections (#978) 0f82420b8 Change cache assets default example to 1 year 676dc6e12 Release 0.61.0 a1d9fb18a Merge commit '79c5d7053486f540b6219e693d5590f2c4c3937a' 1b9e675cd releaser: Add release notes to /docs for release of 0.61.0 5b95cbc8a Fixed suffixIsRemoved error in code samples (#975) 59b2c245b Fix Usage section in toc.md to consider Goldmark TOC settings fd3828673 Add link on how to configure TOC w/ Goldmark ea339f2d6 Added external learning resources including Hugo In Action book. fab502d5d Adjust release notes 269a025ef Release 0.60.1 6bff50bea Merge branch 'temp601' e15251c43 releaser: Add release notes to /docs for release of 0.60.1 690b864b9 Update use-modules.md 345ca8c36 Update syntax-highlighting.md be64af9f5 Update syntax-highlighting.md df9dead83 Update syntax-highlighting.md 1c6e6d238 Fix relnotes 0ea6b2012 Update homepagetweets.toml 2c465625d Update index.md 0025bba09 Update index.md 7417f1c05 Update index.md 9eeb8ce57 Release 0.60.0 25a711023 Merge branch 'temp60' 0c4faab0e releaser: Add release notes to /docs for release of 0.60.0 779e5108e Update installing.md 2ef1c386a Minor text improvements in README.md 41ec5a19a Reword sentence for clarity search.md ded74f098 Add Goldmark as the new default markdown handler cb55cdbb0 Update introduction.md fcaad653a Update homepage.md 7427a1fc5 Update configuration.md 8cc2c72ca tpl/collections: Allow dict to create nested structures 4538c092c Merge commit 'efc0b1bb6c6564f54d596467dbc6a18cb206954e' 90f908e7b Support Go time format strings in permalinks git-subtree-dir: docs git-subtree-split: af4b7ac5b403e392a92228e66058331905fff7dc
142 lines
4.6 KiB
Markdown
142 lines
4.6 KiB
Markdown
---
|
|
title: Syntax Highlighting
|
|
description: Hugo comes with really fast syntax highlighting from Chroma.
|
|
date: 2017-02-01
|
|
publishdate: 2017-02-01
|
|
keywords: [highlighting,chroma,code blocks,syntax]
|
|
categories: [content management]
|
|
menu:
|
|
docs:
|
|
parent: "content-management"
|
|
weight: 300
|
|
weight: 20
|
|
sections_weight: 20
|
|
draft: false
|
|
aliases: [/extras/highlighting/,/extras/highlight/,/tools/syntax-highlighting/]
|
|
toc: true
|
|
---
|
|
|
|
|
|
Hugo uses [Chroma](https://github.com/alecthomas/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](/getting-started/configuration-markup#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:
|
|
|
|
```bash
|
|
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](/content-management/shortcodes/) `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`: Valid values are `true`, `false`, `table`, `inline`. `table` will give copy-and-paste friendly code blocks) turns on line numbers.
|
|
* Setting `linenos` to `false` will turn off linenumbers if it's configured to be on in site config.{{< new-in "0.60.0" >}}
|
|
* `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](/functions/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:
|
|
|
|
```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)
|
|
}
|
|
}
|
|
```
|
|
|
|
{{< 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](https://github.com/yuin/goldmark-highlighting) for more information.
|
|
|
|
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 >}}
|
|
|
|
[Prism]: https://prismjs.com
|
|
[prismdownload]: https://prismjs.com/download.html
|
|
[Highlight.js]: https://highlightjs.org/
|
|
[Rainbow]: https://craig.is/making/rainbows
|
|
[Syntax Highlighter]: https://alexgorbatchev.com/SyntaxHighlighter/
|
|
[Google Prettify]: https://github.com/google/code-prettify
|
|
[Yandex]: https://yandex.ru/
|