Highlight - 高亮代码

highlight 高亮简码使语法突出显示您的代码.

Chroma高亮语言列表
1
print("Hello World!")

用法

这个简码与雨果的完全兼容highlight简码提供一些扩展.

它的调用方式与 Hugo 自己的提供位置参数的简码相同,或者简单地使用代码栅栏.

您也可以自由地从自己的部分调用此简码.在这种情况下,如果您使用兼容性语法将此简码称为部分,则它类似于 Hugo 的 highlight功能 语法.

虽然示例使用带有命名参数的简码,但建议改用代码栅栏.这是因为越来越多的其他软件支持代码栅栏(例如.GitHub),因此您使用Markdown变得更加方便.

```py { lineNos="true" wrap="true" title="python" }
print("Hello World!")
```
{{< highlight lineNos="true" type="py" wrap="true" title="python" >}}
print("Hello World!")
{{< /highlight >}}
{{< highlight py "lineNos=true,wrap=true,title=python" >}}
print("Hello World!")
{{< /highlight >}}
{{ partial "shortcodes/highlight.html" (dict
  "page"    .
  "content" "print(\"Hello World!\")"
  "lineNos" "true"
  "type"    "py"
  "wrap"    "true"
  "title"   "python"
)}}
{{ partial "shortcodes/highlight.html" (dict
  "page"    .
  "content" "print(\"Hello World!\")"
  "options" "lineNos=true,wrap=true,title=python"
  "type"    "py"
)}}

参数

名字 默认值 注释
type <empty> 要高亮显示的代码的语言.从支持的语言中选择一种.不区分大小写.
title <empty> Extension. 代码的任意标题.如果 hl_inline=false(这是雨果默认值),这将像 单个标签 一样显示代码.
wrap 请参阅注释 Extension. 当true时,内容可能会换行,否则它将是可滚动的.

默认值可以在config.toml中设置,并通过前言覆盖.见下文.
options <empty> 零个或多个 HUGO支持的选项 的可选逗号分隔列表以及此表中的扩展参数.
<option> <empty> 任何HUGO支持的选项.
<content> <empty> 要高亮显示的代码.

配置

HUGO支持的选项的默认值可以通过config.toml中的Goldmark设置进行设置

扩展选项的默认值可以通过config.toml中的参数设置进行设置,也可以由每个页面中的前言中覆盖.

全局设置文件

推荐设置

  [markup]
  [markup.highlight]
  # 如果代码换行,表格布局中的行号将会移位,因此最好使用内联;此外,从视觉上看,这两种布局具有相同的外观和行为
  lineNumbersInTable = false

  # 如果 `guessSyntax = true`,即使没有指定语言,也不会有未经样式化的代码,但 Mermaid 和 Math 代码块将不再起作用!因此,如果您想要使用 Mermaid 或 Math 代码块,则这是站点的强制设置
  guessSyntax = false

  # 预装的变体带有自己修改过的 chroma 语法高亮样式,这些样式被导入到 theme-relearn-light.css、theme-relearn-dark.css 等中;
  # 如果您想要使用预定义的样式:
  # - 删除 `noClasses` 或将 `noClasses = true`
  # - 将 `style` 设置为预定义的样式名称
  noClasses = true
  # style = "tango"

可选设置

[params]
  highlightWrap = true

页面的前言

+++
highlightWrap = true
+++

示例

带起始偏移的行号

如上所述,如果代码换行,table 表布局中的行号将移动,因此最好使用内联inline.为了方便起见,请在config.toml中设置lineNumbersInTable = false,并在调用简码时添加lineNos = true,而不是特定值tableinline.

{{< highlight lineNos="true" lineNoStart="666" type="py" >}}
# 最难的部分是开始编写代码;这是一个启动;只需复制并粘贴此内容;它是免费的;接下来的几行会让你付出沉重的代价
print("Hello")
print(" ")
print("World")
print("!")
{{< /highlight >}}
666
667
668
669
670
# 最难的部分是开始编写代码;这是一个启动;只需复制并粘贴此内容;它是免费的;接下来的几行会让你付出沉重的代价
print("Hello")
print(" ")
print("World")
print("!")

带标题的代码围栏

```py { title="python" }
# a bit shorter
print("Hello World!")
```
# a bit shorter
print("Hello World!")

包裹代码(代码换行)

{{< highlight type="py" wrap="true" hl_lines="2" >}}
# 快速排序 Python 单行
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
# 更多的东西
{{< /highlight >}}
# 快速排序 Python 单行
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
# 更多的东西

不包裹(代码不换行)

{{< highlight type="py" wrap="false" hl_lines="2" >}}
# 快速排序 Python 单行
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
# 更多的东西
{{< /highlight >}}
# 快速排序 Python 单行
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
# 更多的东西