HUGO
Menu
GitHub 87548 stars Mastodon

Hugo 语法高亮

为代码示例添加语法高亮。

Hugo 提供多种方法为代码示例添加语法高亮:

围栏代码块

在默认配置中,Hugo 高亮围栏代码块内的代码示例,格式如下:

content/example.md
```LANG [OPTIONS]
CODE
```
CODE
要高亮的代码。
LANG
要高亮的代码语言。从 supported languages 中选择一个。此值不区分大小写。
OPTIONS
一个或多个空格分隔或逗号分隔的键值对,包裹在花括号中。在 site configuration 中为每个选项设置默认值。键名不区分大小写。

例如,使用此 Markdown:

content/example.md
```go {linenos=inline hl_lines=[3,"6-8"] style=emacs}
package main

import "fmt"

func main() {
    for i := 0; i < 3; i++ {
        fmt.Println("Value of i:", i)
    }
}
```

Hugo 渲染这个:

1package main
2
3import "fmt"
4
5func main() {
6    for i := 0; i < 3; i++ {
7        fmt.Println("Value of i:", i)
8    }
9}

选项

anchorLineNos
(bool) 是否将每个行号渲染为 HTML 锚元素,将周围 span 元素的 id 属性设置为行号。如果 lineNosfalse,则此选项无关。默认为 false
codeFences
(bool) 是否高亮代码围栏。默认为 true
guessSyntax
(bool) 如果 LANG 参数为空或设置为没有相应 词法分析器 的语言,是否自动检测语言。如果无法自动检测语言,则回退到纯文本词法分析器。默认为 false

Chroma 语法高亮器包含约 250 种语言的词法分析器,但其中只有 5 种实现了自动语言检测。

hl_Lines
(string) 要在高亮代码中强调的行的空格分隔列表。要强调第 2、3、4 和 7 行,将此值设置为 2-4 7。此选项独立于 lineNoStart 选项。
hl_inline
(bool) 是否在没有包装容器的情况下渲染高亮代码。默认为 false
lineAnchors
(string) 在将行号渲染为 HTML 锚元素时,将此值前置到周围 span 元素的 id 属性。当页面包含两个或更多代码块时,这提供了唯一的 id 属性。如果 lineNosanchorLineNosfalse,则此选项无关。
lineNoStart
(int) 在第一行显示的数字。如果 lineNosfalse,则此选项无关。默认为 1
lineNos
(any) 控制行号显示。默认为 false
  • true:启用行号,由 lineNumbersInTable 控制。
  • false:禁用行号。
  • inline:启用内联行号(将 lineNumbersInTable 设置为 false)。
  • table:启用基于表格的行号(将 lineNumbersInTable 设置为 true)。
lineNumbersInTable
(bool) 是否在 HTML 表格中渲染高亮代码,包含两个单元格。左表格单元格包含行号,右表格单元格包含代码。如果 lineNosfalse,则此选项无关。默认为 true
noClasses
(bool) 是否使用内联 CSS 样式而不是外部 CSS 文件。默认为 true。要使用外部 CSS 文件,将此值设置为 false 并从命令行生成 CSS 文件:
hugo gen chromastyles --style=monokai > syntax.css
style
(string) 应用于高亮代码的 CSS 样式。区分大小写。默认为 monokai。请参阅 语法高亮样式
tabWidth
(int) 用此数量的空格替换高亮代码中的每个制表符。如果 noClassesfalse,则此选项无关。默认为 4
wrapperClass
New in v0.140.2
(string) 用于高亮代码最外层元素的类。默认为 highlight

转义

记录 shortcode 用法时,转义标签分隔符:

content/example.md
```text {linenos=inline}
{{</* shortcode-1 */>}}

{{%/* shortcode-2 */%}}
```

Hugo 渲染这个:

1{{< shortcode-1 >}}
2
3{{% shortcode-2 %}}

语言

以下是支持的语言。在指定语言时使用以下标识符之一,而不是语言名称:

Language Identifiers
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
ATL atl
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
Beef beef
BibTeX bib, bibtex
Bicep bicep
BlitzBasic blitzbasic, b3d, bplus
BNF bnf
BQN bqn
Brainfuck brainfuck, bf
C c
C# csharp, c#
C++ cpp, c++
C3 c3
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
Core core
Crystal cr, crystal
CSS css
CSV csv
CUE cue
Cython cython, pyx, pyrex
D d
Dart dart
Dax dax
Desktop file desktop, desktop_entry
Devicetree devicetree, dts
Diff diff, udiff
Django/Jinja django, jinja
dns zone, bind
Docker docker, dockerfile, containerfile
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
Gemtext gemtext, gmi, gmni, gemini
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
Janet janet
Java java
JavaScript js, javascript
JSON json
JSONata jsonata
Jsonnet jsonnet
Julia julia, jl
Jungle jungle
Kakoune kak, kakoune, kakrc, kakscript
KDL kdl
Kotlin kotlin
Lean4 lean4, lean
Lighttpd configuration file lighty, lighttpd
LLVM llvm
lox
Lua lua, luau
Makefile make, makefile, mf, bsdmake
Mako mako
markdown md, mkd
Markless mess
Mason mason
Materialize SQL dialect materialize, mzsql
Mathematica mathematica, mma, nb
Matlab matlab
MCFunction mcfunction, mcf
Meson meson, meson.build
Metal metal
microcad µcad
MiniZinc minizinc, MZN, mzn
MLIR mlir
Modelica modelica
Modula-2 modula2, m2
Mojo mojo, 🔥
MonkeyC monkeyc
MoonBit moonbit, mbt
MoonScript moonscript, moon
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
NSIS nsis, nsi, nsh
Nu nu
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
Protocol Buffer Text Format txtpb
PRQL prql
PSL psl
Puppet puppet
Python python, py, sage, python3, py3, starlark
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
RGBDS Assembly rgbasm
Ring ring
RPGLE SQLRPGLE, RPG IV
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
SNBT snbt
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, hcl
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
Typst typst
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
WebAssembly Text Format wast, wat
WebGPU Shading Language wgsl
WebVTT vtt
Whiley whiley
XML xml
Xorg xorg.conf
YAML yaml
YANG yang
Z80 Assembly z80
Zed zed
Zig zig

Last updated: January 1, 0001
Improve this page