RENDER HOOKS

Blockquote render hooks

New in v0.132.0

背景

引用區塊渲染掛載範本會接收以下 上下文:

AlertType

(string)當 Type 為 alert 時適用,這是轉換為小寫的警示類型。請參考下方的 警示 區段。

AlertTitle
New in v0.134.0

(template.HTML)當 Type 為 alert 時適用,這是警示標題。請參考下方的 警示 區段。

AlertSign
New in v0.134.0

(string)當 Type 為 alert 時適用,這是警示符號。通常用於表示警示是否可視覺折疊,可能的值為 +、 - 或空字串。請參考下方的 警示 區段。

Attributes

(map)Markdown 屬性,若您的網站設定如下則可用:

hugo.
     
markup:
  goldmark:
    parser:
      attribute:
        block: true
[markup]
  [markup.goldmark]
    [markup.goldmark.parser]
      [markup.goldmark.parser.attribute]
        block = true
{
   "markup": {
      "goldmark": {
         "parser": {
            "attribute": {
               "block": true
            }
         }
      }
   }
}
Ordinal

(int)頁面中引用區塊的零基序號。

Page

(page)當前頁面的參考。

PageInner

(page)通過 RenderShortcodes 方法嵌套的頁面參考。查看詳細資訊。

Position

(string)引用區塊在頁面內容中的位置。

Text

(template.HTML)引用區塊文字,若 Type 為 alert 則不包含第一行文字。請參考下方的 警示 區段。

Type

(bool)引用區塊類型。如果引用區塊有警示標記則返回 alert,否則為 regular。請參考下方的 警示 區段。

範例

在預設設定中,Hugo 根據 CommonMark 規範 渲染 Markdown 引用區塊。以下是創建執行相同行為的渲染掛載範例:

layouts/_default/_markup/render-blockquote.html
<blockquote>
  {{ .Text }}
</blockquote>

若要將引用區塊渲染為包含可選引用和標題的 HTML figure 元素:

layouts/_default/_markup/render-blockquote.html
<figure>
  <blockquote {{ with .Attributes.cite }}cite="{{ . }}"{{ end }}>
    {{ .Text }}
  </blockquote>
  {{ with .Attributes.caption }}
    <figcaption class="blockquote-caption">
      {{ . | safeHTML }}
    </figcaption>
  {{ end }}
</figure>

然後在您的 Markdown 中:

> Some text
{cite="https://gohugo.io" caption="Some caption"}

警示

也稱為 提示 或 註解,警示是用於強調關鍵資訊的引用區塊。

基本語法

在基本 Markdown 語法中,每個警示的第一行為警示標記,由一個感嘆號後跟警示類型組成,並包裹於方括號中。例如:

content/example.md
> [!NOTE]
> Useful information that users should know, even when skimming content.

> [!TIP]
> Helpful advice for doing things better or more easily.

> [!IMPORTANT]
> Key information users need to know to achieve their goal.

> [!WARNING]
> Urgent info that needs immediate user attention to avoid problems.

> [!CAUTION]
> Advises about risks or negative outcomes of certain actions.

基本語法與 GitHub、Obsidian 和 Typora 兼容。

擴展語法

在擴展 Markdown 語法中,您可以選擇性地包括警示符號和/或警示標題。警示符號通常為 + 或 -,用於表示警示是否可折疊。例如:

content/example.md
> [!WARNING]+ Radiation hazard
> Do not approach or handle without protective gear.

擴展語法與 Obsidian 兼容。

範例

此引用區塊渲染掛載若檢測到警示標記,則渲染多語言警示;否則根據 CommonMark 規範渲染引用區塊。

layouts/_default/_markup/render-blockquote.html
{{ $emojis := dict
  "caution" ":exclamation:"
  "important" ":information_source:"
  "note" ":information_source:"
  "tip" ":bulb:"
  "warning" ":information_source:"
}}

{{ if eq .Type "alert" }}
  <blockquote class="alert alert-{{ .AlertType }}">
    <p class="alert-heading">
      {{ transform.Emojify (index $emojis .AlertType) }}
      {{ with .AlertTitle }}
        {{ . }}
      {{ else }}
        {{ or (i18n .AlertType) (title .AlertType) }}
      {{ end }}
    </p>
    {{ .Text }}
  </blockquote>
{{ else }}
  <blockquote>
    {{ .Text }}
  </blockquote>
{{ end }}

要覆蓋標籤,可在您的 i18n 檔案中新增這些條目:

i18n/en.toml.
     
caution: Caution
important: Important
note: Note
tip: Tip
warning: Warning
caution = 'Caution'
important = 'Important'
note = 'Note'
tip = 'Tip'
warning = 'Warning'
{
   "caution": "Caution",
   "important": "Important",
   "note": "Note",
   "tip": "Tip",
   "warning": "Warning"
}

雖然可以使用如上所示的條件邏輯範本,但也可以為每種類型的引用區塊創建單獨的範本:

layouts/
└── _default/
    └── _markup/
        ├── render-blockquote-alert.html
        └── render-blockquote-regular.html

PageInner 詳細資訊

New in v0.125.0

PageInner 的主要用途是解析相對於包含的 Page 的連結和 頁面資源。例如,建立一個「include」掛載程式碼,從多個內容檔案組合成一個頁面,同時保留註腳和目錄的全域上下文:

layouts/shortcodes/include.html
{{ with site.GetPage (.Get 0) }}
  {{ .RenderShortcodes }}
{{ end }}

然後在您的 Markdown 中呼叫此掛載程式碼:

content/posts/p1.md
{{% include "/posts/p2" %}}

在渲染 /posts/p2 時觸發的任何渲染掛載將獲得:

  • /posts/p1 當呼叫 Page
  • /posts/p2 當呼叫 PageInner

如果不相關,PageInner 將回退到 Page 的值,並始終返回一個值。

作為一個實際範例,Hugo 的內嵌連結和圖片渲染掛載使用 PageInner 方法解析 Markdown 中連結和圖片的目標位置。請參考以下來源程式碼: