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