Syntax highlighting
Hugo 使用 Chroma 作為其代碼高亮工具;它是用 Go 語言編寫的,速度非常快。
配置語法高亮工具
請參閱 配置高亮。
生成語法高亮 CSS
如果您的站點配置中設置了 markup.highlight.noClasses=false
,您需要一個樣式表。這個樣式表將覆蓋 markup.highlight.style
中指定的樣式。
您可以通過 Hugo 生成:
hugo gen chromastyles --style=monokai > syntax.css
運行 hugo gen chromastyles -h
以獲取更多選項。請參見 https://xyproto.github.io/splash/docs/ 查看可用樣式的圖庫。
highlight 短代碼
語法高亮通過內建的 highlight
短代碼 實現。它需要一個必填的參數,用於指定要高亮顯示的編程語言,並且需要一個關閉標籤。
選項:
linenos
: 配置行號。有效值為true
、false
、table
或inline
。如果在站點配置中將其設為啟用,false
將關閉行號。table
會生成便於複製和粘貼的代碼區塊。hl_lines
: 列出要高亮顯示的行號或行號範圍。linenostart=199
: 從第 199 行開始計算行號。anchorlinenos
: 配置行號的錨點。有效值為true
或false
。lineanchors
: 配置行號的錨點前綴。將會附加-
,例如,將lineanchors=prefix
配置為 1 行的錨點會顯示為prefix-1
。hl_inline
:高亮內嵌<code>
(行內 HTML 元素)標籤。有效值為true
或false
。code
標籤將會得到code-inline
類別。
範例:highlight 短代碼
{{< highlight go "linenos=table,hl_lines=8 15-17,linenostart=199" >}}
// ... 代碼
{{< / highlight >}}
結果:
|
|
高亮顯示 Hugo/Go 模板代碼
要高亮顯示 Hugo/Go 模板代碼,請在開頭的雙大括號後添加 /*
,並在關閉大括號前加上 */
。
{{</* myshortcode */>}}
結果:
{{< myshortcode >}}
高亮顯示模板函數
請參閱 高亮顯示。
在代碼框中高亮顯示
在代碼框中高亮顯示默認是啟用的。
```go {linenos=table,hl_lines=[8,"15-17"],linenostart=199}
// ... 代碼
```
結果:
|
|
選項與 highlighting shortcode 中的選項相同,包括 linenos=false
,但請注意,Markdown 屬性語法略有不同。
Chroma 語法高亮支持的語言列表
Chroma 語法高亮支持的完整語法分析器及其別名(這些別名在 highlight
模板函數或代碼框中使用):
- ABAP
- abap
- ABNF
- abnf
- ActionScript
- as, actionscript
- ActionScript 3
- as3, actionscript3
- Ada
- ada, ada95, ada2005
- Agda
- agda
- AL
- al
- Alloy
- alloy
- Angular2
- ng2
- ANTLR
- antlr
- ApacheConf
- apacheconf, aconf, apache
- APL
- apl
- AppleScript
- applescript
- ArangoDB AQL
- aql
- Arduino
- arduino
- ArmAsm
- armasm
- AutoHotkey
- autohotkey, ahk
- AutoIt
- autoit
- Awk
- awk, gawk, mawk, nawk
- Ballerina
- ballerina
- Bash
- bash, sh, ksh, zsh, shell
- Bash Session
- bash-session, console, shell-session
- Batchfile
- bat, batch, dosbatch, winbatch
- BibTeX
- bib, bibtex
- Bicep
- bicep
- BlitzBasic
- blitzbasic, b3d, bplus
- BNF
- bnf
- BQN
- bqn
- Brainfuck
- brainfuck, bf
- C
- c
- C#
- csharp, c#
- C++
- cpp, c++
- Caddyfile
- caddyfile, caddy
- Caddyfile Directives
- caddyfile-directives, caddyfile-d, caddy-d
- Cap'n Proto
- capnp
- Cassandra CQL
- cassandra, cql
- Ceylon
- ceylon
- CFEngine3
- cfengine3, cf3
- cfstatement
- cfs
- ChaiScript
- chai, chaiscript
- Chapel
- chapel, chpl
- Cheetah
- cheetah, spitfire
- Clojure
- clojure, clj, edn
- CMake
- cmake
- COBOL
- cobol
- CoffeeScript
- coffee-script, coffeescript, coffee
- Common Lisp
- common-lisp, cl, lisp
- Coq
- coq
- Crystal
- cr, crystal
- CSS
- css
- CUE
- cue
- Cython
- cython, pyx, pyrex
- D
- d
- Dart
- dart
- Dax
- dax
- Desktop file
- desktop, desktop_entry
- Diff
- diff, udiff
- Django/Jinja
- django, jinja
- dns
- zone, bind
- Docker
- docker, dockerfile
- DTD
- dtd
- Dylan
- dylan
- EBNF
- ebnf
- Elixir
- elixir, ex, exs
- Elm
- elm
- EmacsLisp
- emacs, elisp, emacs-lisp
- Erlang
- erlang
- Factor
- factor
- Fennel
- fennel, fnl
- Fish
- fish, fishshell
- Forth
- forth
- Fortran
- fortran, f90
- FortranFixed
- fortranfixed
- FSharp
- fsharp
- GAS
- gas, asm
- GDScript
- gdscript, gd
- GDScript3
- gdscript3, gd3
- Genshi
- genshi, kid, xml+genshi, xml+kid
- Genshi HTML
- html+genshi, html+kid
- Genshi Text
- genshitext
- Gherkin
- cucumber, Cucumber, gherkin, Gherkin
- Gleam
- gleam>
- GLSL
- glsl
- Gnuplot
- gnuplot
- Go
- go, golang
- Go HTML Template
- go-html-template
- Go Template
- go-template
- Go Text Template
- go-text-template
- GraphQL
- graphql, graphqls, gql
- Groff
- groff, nroff, man
- Groovy
- groovy
- Handlebars
- handlebars, hbs
- Hare
- hare
- Haskell
- haskell, hs
- Haxe
- hx, haxe, hxsl
- HCL
- hcl
- Hexdump
- hexdump
- HLB
- hlb
- HLSL
- hlsl
- HolyC
- holyc
- HTML
- html
- HTTP
- http
- Hy
- hylang
- Idris
- idris, idr
- Igor
- igor, igorpro
- INI
- ini, cfg, dosini
- Io
- io
- ISCdhcpd
- iscdhcpd
- J
- j
- Java
- java
- JavaScript
- js, javascript
- JSON
- json
- Julia
- julia, jl
- Jungle
- jungle
- Kotlin
- kotlin
- Lighttpd configuration file
- lighty, lighttpd
- LLVM
- llvm
- Lua
- lua
- Makefile
- make, makefile, mf, bsdmake
- Mako
- mako
- markdown
- md, mkd
- Mason
- mason
- Materialize SQL dialect
- materialize, mzsql
- Mathematica
- mathematica, mma, nb
- Matlab
- matlab
- mcfunction
- mcfunction
- Meson
- meson, meson.build
- Metal
- metal
- MiniZinc
- minizinc, MZN, mzn
- MLIR
- mlir
- Modula-2
- modula2, m2
- MonkeyC
- monkeyc
- MorrowindScript
- morrowind, mwscript
- Myghty
- myghty
- MySQL
- mysql, mariadb
- NASM
- nasm
- Natural
- natural
- NDISASM
- ndisasm
- Newspeak
- newspeak
- Nginx configuration file
- nginx
- Nim
- nim, nimrod
- Nix
- nixos, nix
- Objective-C
- objective-c, objectivec, obj-c, objc
- ObjectPascal
- objectpascal
- OCaml
- ocaml
- Octave
- octave
- Odin
- odin
- OnesEnterprise
- ones, onesenterprise, 1S, 1S:Enterprise
- OpenEdge ABL
- openedge, abl, progress, openedgeabl
- OpenSCAD
- openscad
- Org Mode
- org, orgmode
- PacmanConf
- pacmanconf
- Perl
- perl, pl
- PHP
- php, php3, php4, php5
- PHTML
- phtml
- Pig
- pig
- PkgConfig
- pkgconfig
- PL/pgSQL
- plpgsql
- plaintext
- text, plain, no-highlight
- Plutus Core
- plutus-core, plc
- Pony
- pony
- PostgreSQL SQL dialect
- postgresql, postgres
- PostScript
- postscript, postscr
- POVRay
- pov
- PowerQuery
- powerquery, pq
- PowerShell
- powershell, posh, ps1, psm1, psd1, pwsh
- Prolog
- prolog
- Promela
- promela
- PromQL
- promql
- properties
- java-properties
- Protocol Buffer
- protobuf, proto
- PRQL
- prql
- PSL
- psl
- Puppet
- puppet
- Python
- python, py, sage, python3, py3
- Python 2
- python2, py2
- QBasic
- qbasic, basic
- QML
- qml, qbs
- R
- splus, s, r
- Racket
- racket, rkt
- Ragel
- ragel
- Raku
- perl6, pl6, raku
- react
- jsx, react
- ReasonML
- reason, reasonml
- reg
- registry
- Rego
- rego
- reStructuredText
- rst, rest, restructuredtext
- Rexx
- rexx, arexx
- RPMSpec
- spec
- Ruby
- rb, ruby, duby
- Rust
- rust, rs
- SAS
- sas
- Sass
- sass
- Scala
- scala
- Scheme
- scheme, scm
- Scilab
- scilab
- SCSS
- scss
- Sed
- sed, gsed, ssed
- Sieve
- sieve
- Smali
- smali
- Smalltalk
- smalltalk, squeak, st
- Smarty
- smarty
- Snobol
- snobol
- Solidity
- sol, solidity
- SourcePawn
- sp
- SPARQL
- sparql
- SQL
- sql
- SquidConf
- squidconf, squid.conf, squid
- Standard ML
- sml
- stas
- Stylus
- stylus
- Svelte
- svelte
- Swift
- swift
- SYSTEMD
- systemd
- systemverilog
- systemverilog, sv
- TableGen
- tablegen
- Tal
- tal, uxntal
- TASM
- tasm
- Tcl
- tcl
- Tcsh
- tcsh, csh
- Termcap
- termcap
- Terminfo
- terminfo
- Terraform
- terraform, tf
- TeX
- tex, latex
- Thrift
- thrift
- TOML
- toml
- TradingView
- tradingview, tv
- Transact-SQL
- tsql, t-sql
- Turing
- turing
- Turtle
- turtle
- Twig
- twig
- TypeScript
- ts, tsx, typescript
- TypoScript
- typoscript
- TypoScriptCssData
- typoscriptcssdata
- TypoScriptHtmlData
- typoscripthtmldata
- ucode
- V
- v, vlang
- V shell
- vsh, vshell
- Vala
- vala, vapi
- VB.net
- vb.net, vbnet
- verilog
- verilog, v
- VHDL
- vhdl
- VHS
- vhs, tape, cassette
- VimL
- vim
- vue
- vue, vuejs
- WDTE
- WebGPU Shading Language
- wgsl
- Whiley
- whiley
- XML
- xml
- Xorg
- xorg.conf
- YAML
- yaml
- YANG
- yang
- Z80 Assembly
- z80
- Zed
- zed
- Zig
- zig