HUGO

  • 新闻
  • 文档
  • 主题
  • 作品展示
  • 社区
  • GitHub
    • English
    • 中文

What's on this Page

  • 配置语法高亮
  • 生成语法高亮CSS
  • 亮点简码
    • 例如:突出显示简码
  • 突出显示模板函数功能
  • 凸显代码栅栏
  • 色度高亮显示的语言列表
CONTENT MANAGEMENT

语法高亮

Hugo comes with really fast syntax highlighting from Chroma.

Hugo uses 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.

配置语法高亮

See Configure Highlight.

生成语法高亮CSS

If you run with pygmentsUseClasses=true in your site config, you need a style sheet.

You can generate one with Hugo:

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.

亮点简码

Highlighting is carried out via the built-in shortcode 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.
  • hl_lines lists a set of line numbers or line number ranges to be highlighted.
  • linenostart=199 starts the line number count from 199.

例如:突出显示简码

{{< highlight go "linenos=table,hl_lines=8 15-17,linenostart=199" >}}
// ... code
{{< / highlight >}}

Gives this:

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// 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)
  }
}

突出显示模板函数功能

See Highlight.

凸显代码栅栏

Highlighting in code fences is enabled by default.

```go {linenos=table,hl_lines=[8,"15-17"],linenostart=199}
// ... code
```

Gives this:

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// 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)
  }
}

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 for more information.

The options are the same as in the highlighting shortcode,including linenos=false, but note the slightly different Markdown attribute syntax.

色度高亮显示的语言列表

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):

ABAP
ABAP, abap
ABNF
abnf
ANTLR
antlr
APL
apl
ActionScript
actionscript, as
ActionScript 3
actionscript3, as, as3
Ada
ada, ada2005, ada95, adb, ads
Angular2
ng2
ApacheConf
aconf, apache, apacheconf, conf, htaccess
AppleScript
applescript
Arduino
arduino, ino
Awk
awk, gawk, mawk, nawk
BNF
bnf
Ballerina
bal, ballerina
Base Makefile
*, bsdmake, mak, make, makefile, mf, mk
Bash
bash, bash_*, bashrc, ebuild, eclass, exheres-0, exlib, ksh, sh, shell, zsh, zshrc
Batchfile
bat, batch, cmd, dosbatch, winbatch
BibTeX
bib, bibtex
BlitzBasic
b3d, bb, blitzbasic, bplus, decls
Brainfuck
b, bf, brainfuck
C
c, h, idc
C#
c#, cs, csharp
C++
C, CPP, H, c++, cc, cp, cpp, cxx, h++, hh, hpp, hxx
CFEngine3
cf, cf3, cfengine3
CMake
cmake, txt
COBOL
COB, CPY, cob, cobol, cpy
CSS
css
Cap'n Proto
capnp
Cassandra CQL
cassandra, cql
Ceylon
ceylon
ChaiScript
chai, chaiscript
Cheetah
cheetah, spitfire, spt, tmpl
Clojure
clj, clojure
CoffeeScript
coffee, coffee-script, coffeescript
Common Lisp
cl, common-lisp, lisp
Coq
coq, v
Crystal
cr, crystal
Cython
cython, pxd, pxi, pyrex, pyx
D
d, di
DTD
dtd
Dart
dart
Diff
diff, patch, udiff
Django/Jinja
django, jinja
Docker
docker, dockerfile
EBNF
ebnf
Elixir
elixir, ex, exs
Elm
elm
EmacsLisp
el, elisp, emacs, emacs-lisp
Erlang
erl, erlang, es, escript, hrl
FSharp
fs, fsharp, fsi
Factor
factor
Fish
fish, fishshell, load
Forth
forth, frt, fs, fth
Fortran
F03, F90, f03, f90, fortran
GAS
S, asm, gas, s
GDScript
gd, gdscript
GLSL
frag, geo, glsl, vert
Genshi
genshi, kid, xml+genshi, xml+kid
Genshi HTML
html+genshi, html+kid
Genshi Text
genshitext
Gnuplot
gnuplot, plot, plt
Go
go, golang
Go HTML Template
go-html-template
Go Text Template
go-text-template
GraphQL
gql, graphql, graphqls
Groovy
gradle, groovy
HCL
hcl
HTML
htm, html, xhtml, xslt
HTTP
http
Handlebars
handlebars
Haskell
haskell, hs
Haxe
haxe, hx, hxsl
Hexdump
hexdump
Hy
hy, hylang
INI
cfg, dosini, gitconfig, inf, ini
Idris
idr, idris
Igor
igor, igorpro, ipf
Io
io
J
ijs, j
JSON
json
Java
java
JavaScript
javascript, js, jsm
Julia
jl, julia
Jungle
jungle
Kotlin
kotlin, kt
LLVM
ll, llvm
Lighttpd configuration file
lighttpd, lighty
Lua
lua, wlua
MLIR
mlir
Mako
mako, mao
Mason
m, mason, mc, mhtml, mi
Mathematica
cdf, ma, mathematica, mma, nb, nbp
Matlab
m, matlab
MiniZinc
MZN, dzn, fzn, minizinc, mzn
Modula-2
def, m2, mod, modula2
MonkeyC
mc, monkeyc
MorrowindScript
morrowind, mwscript
MySQL
mysql, sql
Myghty
myghty, myt
NASM
ASM, asm, nasm
Newspeak
newspeak, ns2
Nginx configuration file
conf, nginx
Nim
nim, nimrod
Nix
nix, nixos
OCaml
ml, mli, mll, mly, ocaml
Objective-C
h, m, obj-c, objc, objective-c, objectivec
Octave
m, octave
OpenSCAD
openscad, scad
Org Mode
org, orgmode
PHP
inc, php, php3, php4, php5, php[345]
PL/pgSQL
plpgsql
POVRay
inc, pov
PacmanConf
conf, pacmanconf
Perl
perl, pl, pm, t
Pig
pig
PkgConfig
pc, pkgconfig
PostScript
eps, postscr, postscript, ps
PostgreSQL SQL dialect
postgres, postgresql
PowerShell
posh, powershell, ps1, psm1
Prolog
ecl, pl, pro, prolog
Protocol Buffer
proto, protobuf
Puppet
pp, puppet
Python
py, python, pyw, sage, sc, tac
Python 3
py3, python3
QBasic
BAS, bas, basic, qbasic
R
R, Renviron, Rhistory, Rprofile, S, r, s, splus
Racket
racket, rkt, rktd, rktl
Ragel
ragel
Rexx
arexx, rex, rexx, rx
Ruby
duby, gemspec, rake, rb, rbw, rbx, ruby
Rust
in, rs, rust
SCSS
scss
SPARQL
rq, sparql
SQL
sql
SYSTEMD
service, systemd
Sass
sass
Scala
scala
Scheme
scheme, scm, ss
Scilab
sce, sci, scilab, tst
Smalltalk
smalltalk, squeak, st
Smarty
smarty, tpl
Snobol
snobol
Solidity
sol, solidity
SquidConf
conf, squid, squid.conf, squidconf
Standard ML
fun, sig, sml
Swift
swift
TASM
ASM, asm, tasm
TOML
toml
TableGen
tablegen, td
Tcl
rvt, tcl
Tcsh
csh, tcsh
TeX
aux, latex, tex, toc
Termcap
src, termcap
Terminfo
src, terminfo
Terraform
terraform, tf
Thrift
thrift
TradingView
tradingview, tv
Transact-SQL
t-sql, tsql
Turing
tu, turing
Turtle
ttl, turtle
Twig
twig
TypeScript
ts, tsx, typescript
TypoScript
ts, txt, typoscript
TypoScriptCssData
typoscriptcssdata
TypoScriptHtmlData
typoscripthtmldata
VB.net
bas, vb, vb.net, vbnet
VHDL
vhd, vhdl
VimL
exrc, gvimrc, vim, vimrc
WDTE
wdte
XML
rss, svg, wsdl, wsf, xml, xsd, xsl, xslt
Xorg
conf, xorg.conf
YAML
yaml, yml
cfstatement
cfs
markdown
markdown, md, mkd
plaintext
no-highlight, plain, text, txt
reStructuredText
rest, restructuredtext, rst
react
jsx, react
reg
reg, registry
systemverilog
sv, svh, systemverilog
verilog
v, verilog
vue
vue, vuejs

See Also

  • highlight
  • 配置标记
  • 关于 Hugo
    • 概述
    • Hugo的安全模型
    • Hugo and GDPR
    • 什么是Hugo
    • Hugo 特征
    • 静态的好处
    • 证书
  • 入门
    • 入门概述
    • 快速开始
    • 安装 Hugo
    • 基本用法
    • 目录结构
    • 配置
    • 外部学习资源
  • Hugo 模块
    • Hugo 模块概述
    • 配置模块
    • 使用Hugo模块
    • 主题组件
  • 内容管理
    • 内容管理概述
    • 组织
    • 捆绑页
    • 内容格式
    • 前面的问题
    • 构建选项
    • 网页资源
    • 图像处理
    • 简码
    • 相关内容
    • 章节
    • 内容类型
    • 原型
    • 分类
    • 摘要
    • 链接和交叉引用
    • URL管理
    • 菜单
    • 目录
    • 静态文件
    • 注释
    • 多种语言 和 国际化
    • 语法高亮
  • 模板
    • 模板概述
    • 介绍
    • 模板查找顺序
    • 自定义输出格式
    • 基本模板和模块
    • 列表页面模板
    • 首页模板
    • 章节模板
    • 分类模板
    • 单页模板
    • 内容视图模板
    • 数据模板
    • 部件模板
    • 简码模板
    • 本地文件模板
    • 404页
    • 菜单模板
    • 分页
    • RSS 模板
    • 网站地图模板
    • Robots.txt
    • 内置模板
    • 另类模板
    • 模板调试
  • 函数
    • 函数快速参考
    • .AddDate
    • .Format
    • .Get
    • .GetPage
    • .HasMenuCurrent
    • .IsMenuCurrent
    • .Param
    • .Render
    • .RenderString
    • .Scratch
    • .Unix
    • absLangURL
    • absURL
    • after
    • anchorize
    • append
    • apply
    • base64
    • chomp
    • complement
    • cond
    • countrunes
    • countwords
    • dateFormat
    • default
    • delimit
    • dict
    • echoParam
    • emojify
    • eq
    • errorf and warnf
    • fileExists
    • findRE
    • first
    • float
    • ge
    • getenv
    • group
    • gt
    • hasPrefix
    • highlight
    • htmlEscape
    • htmlUnescape
    • hugo
    • humanize
    • i18n
    • in
    • index
    • int
    • intersect
    • isset
    • jsonify
    • lang.Merge
    • lang.NumFmt
    • last
    • le
    • lower
    • lt
    • markdownify
    • Math
    • md5
    • merge
    • ne
    • now
    • os.Stat
    • partialCached
    • path.Base
    • path.Dir
    • path.Ext
    • path.Join
    • path.Split
    • plainify
    • pluralize
    • print
    • printf
    • println
    • querify
    • range
    • readDir
    • readFile
    • ref
    • reflect.IsMap
    • reflect.IsSlice
    • relLangURL
    • relref
    • relURL
    • replace
    • replaceRE
    • safeCSS
    • safeHTML
    • safeHTMLAttr
    • safeJS
    • safeURL
    • seq
    • sha
    • shuffle
    • singularize
    • slice
    • slicestr
    • sort
    • split
    • string
    • strings.HasSuffix
    • strings.Repeat
    • strings.RuneCount
    • strings.TrimLeft
    • strings.TrimPrefix
    • strings.TrimRight
    • strings.TrimSuffix
    • substr
    • symdiff
    • templates.Exists
    • time
    • title
    • transform.Unmarshal
    • trim
    • truncate
    • union
    • uniq
    • upper
    • urlize
    • urls.Parse
    • where
    • with
    • 图片函数
  • 变量
    • 变量概述
    • 网站变量
    • 简码变量
    • 页面变量
    • 页面方法
    • 分类变量
    • 文件变量
    • 菜单项属性
    • Hugo 变量
    • Git的变量
    • 网站地图变量
  • Hugo 管道
    • Hugo 管道概述
    • Hugo 管道简介
    • SASS / SCSS
    • PostCSS
    • 资产压缩
    • Asset 捆绑
    • 指纹和SRI
    • 来自模板资源
    • 从字符串资源
  • CLI
  • 故障排除
    • 疑难解答
    • FAQ
    • 构建性能
  • 工具
    • 开发工具概述
    • 迁移
    • 入门套件
    • 前端
    • 编辑器插件
    • 搜索
    • 其他的项目
  • 托管和部署
    • 托管和部署概述
    • Hugo 部署
    • 使用Nanobox的主机无关部署
    • AWS Amplify托管
    • Netlify托管
    • Render托管
    • Firebase托管
    • GitHub托管
    • GitLab托管
    • KeyCDN托管
    • Bitbucket托管
    • 使用Wercker部署
    • 使用rsync部署
  • 贡献
    • 贡献Hugo
    • 开发
    • 文档
    • 主题
  • 保养
“语法高亮” was last updated: March 24, 2020: 添加翻译 (d6d8ad2)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • Discuss Source Code
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 
 

Hugo Sponsors

Logo for Forestry.io
Logo for Linode
Logo for eSolia
 

The Hugo logos are copyright © Steve Francia 2013–2020.

The Hugo Gopher is based on an original work by Renée French.

  • 新闻
  • 文档
  • 主题
  • 作品展示
  • 社区
  • GitHub
  • 关于 Hugo
  • 入门
  • Hugo 模块
  • 内容管理
  • 模板
  • 函数
  • 变量
  • Hugo 管道
  • CLI
  • 故障排除
  • 工具
  • 托管和部署
  • 贡献
  • 保养