Links and cross references
ref
和 relref
短代碼分別顯示文檔的絕對和相對永久鏈接。
使用 ref
和 relref
ref
和 relref
短代碼需要一個參數:指向內容文檔的路徑,路徑可以有也可以沒有文件擴展名,並且可以包含或不包含錨點。沒有以 /
開頭的路徑會先解析為相對於當前頁面的路徑,然後再解析為網站的其他部分。
.
└── content
├── about
| ├── _index.md
| └── credits.md
├── pages
| ├── document1.md
| └── document2.md // 包含錨點 #anchor
├── products
| └── index.md
└── blog
└── my-post.md
這些頁面可以如下引用:
{{< ref "document2" >}} <-- 從 pages/document1.md,相對路徑
{{< ref "document2#anchor" >}}
{{< ref "document2.md" >}}
{{< ref "document2.md#anchor" >}}
{{< ref "#anchor" >}} <-- 從 pages/document2.md
{{< ref "/blog/my-post" >}} <-- 從任何地方,絕對路徑
{{< ref "/blog/my-post.md" >}}
{{< relref "document" >}}
{{< relref "document.md" >}}
{{< relref "#anchor" >}}
{{< relref "/blog/my-post.md" >}}
index.md
可以根據其路徑或其包含的資料夾來引用,並且不需要結尾的 /
。_index.md
只能通過其包含的資料夾來引用:
{{< ref "/about" >}} <-- 引用 /about/_index.md
{{< ref "/about/_index" >}} <-- 會引發 REF_NOT_FOUND 錯誤
{{< ref "/about/credits.md" >}} <-- 引用 /about/credits.md
{{< ref "/products" >}} <-- 引用 /products/index.md
{{< ref "/products/index" >}} <-- 引用 /products/index.md
要在 Markdown 中使用 ref
或 relref
生成超鏈接:
[About]({{< ref "/about" >}} "關於我們")
如果無法唯一解析文檔,Hugo 會發出錯誤或警告。錯誤行為是可配置的;請參閱下文。
連結到另一種語言版本
在不指定語言的情況下使用 ref
或 relref
,則會使參照解析為當前內容頁面的語言。
要鏈接到文檔的另一種語言版本,請使用以下語法:
{{< relref path="document.md" lang="ja" >}}
獲取另一種輸出格式
要鏈接到文檔的另一種輸出格式,請使用以下語法:
{{< relref path="document.md" outputFormat="rss" >}}
標題 ID
使用 Markdown 文檔類型時,Hugo 會為頁面上的每個標題生成元素 ID。例如:
## Reference
會生成如下 HTML:
<h2 id="reference">Reference</h2>
通過將 ID 附加到路徑上,可以在使用 ref
或 relref
短代碼時獲得指向標題的永久鏈接:
{{< ref "document.md#reference" >}}
{{< relref "document.md#reference" >}}
通過包含屬性來生成自定義標題 ID。例如:
## Reference A {#foo}
## Reference B {id="bar"}
會生成如下 HTML:
<h2 id="foo">Reference A</h2>
<h2 id="bar">Reference B</h2>
如果同一標題在頁面上出現多次,Hugo 會生成唯一的元素 ID。例如:
## Reference
## Reference
## Reference
會生成如下 HTML:
<h2 id="reference">Reference</h2>
<h2 id="reference-1">Reference</h2>
<h2 id="reference-2">Reference</h2>
Ref 和 RelRef 配置
可以在 hugo.toml
中配置此行為:
refLinksErrorLevel
(“ERROR”)- 當使用
ref
或relref
解析頁面鏈接時,如果無法解析鏈接,將使用此日誌級別記錄。有效值為ERROR
(預設)或WARNING
。任何ERROR
都會導致構建失敗(exit -1
)。 refLinksNotFoundURL
- 當在
ref
或relref
中找不到頁面參照時,作為佔位符使用的 URL。直接按原樣使用。