HUGO
Menu
GitHub 87548 stars Mastodon

Hugo 配置相關內容

Hugo 配置相關內容。

要了解 Hugo 的相關內容識別,請參閱 相關內容 頁面。

Hugo 提供了一個合理的默認配置來識別相關內容,但您可以在站點配置中自定義它,可以全局或按語言進行。

默認配置

這是默認配置:

related:
  includeNewer: false
  indices:
  - applyFilter: false
    cardinalityThreshold: 0
    name: keywords
    pattern: ''
    toLower: false
    type: basic
    weight: 100
  - applyFilter: false
    cardinalityThreshold: 0
    name: date
    pattern: ''
    toLower: false
    type: basic
    weight: 10
  - applyFilter: false
    cardinalityThreshold: 0
    name: tags
    pattern: ''
    toLower: false
    type: basic
    weight: 80
  threshold: 80
  toLower: false
[related]
  includeNewer = false
  threshold = 80
  toLower = false
  [[related.indices]]
    applyFilter = false
    cardinalityThreshold = 0
    name = 'keywords'
    pattern = ''
    toLower = false
    type = 'basic'
    weight = 100
  [[related.indices]]
    applyFilter = false
    cardinalityThreshold = 0
    name = 'date'
    pattern = ''
    toLower = false
    type = 'basic'
    weight = 10
  [[related.indices]]
    applyFilter = false
    cardinalityThreshold = 0
    name = 'tags'
    pattern = ''
    toLower = false
    type = 'basic'
    weight = 80
{
   "related": {
      "includeNewer": false,
      "indices": [
         {
            "applyFilter": false,
            "cardinalityThreshold": 0,
            "name": "keywords",
            "pattern": "",
            "toLower": false,
            "type": "basic",
            "weight": 100
         },
         {
            "applyFilter": false,
            "cardinalityThreshold": 0,
            "name": "date",
            "pattern": "",
            "toLower": false,
            "type": "basic",
            "weight": 10
         },
         {
            "applyFilter": false,
            "cardinalityThreshold": 0,
            "name": "tags",
            "pattern": "",
            "toLower": false,
            "type": "basic",
            "weight": 80
         }
      ],
      "threshold": 80,
      "toLower": false
   }
}

向站點配置添加 related 部分需要您提供完整的配置。您不能在不指定所有相關設置的情況下覆蓋單個默認值。

頂級選項

threshold
(int) 0-100 之間的值。較低的值將返回更多但可能不那麼相關的匹配。
includeNewer
(bool) 是否在相關內容列表中包含比當前頁面更新的頁面。這意味著較舊帖子的輸出可能會隨著添加新相關內容而改變。默認值是 false
toLower
(bool) 是否將索引和查詢中的關鍵字轉換為小寫。這可能會以更輕微的性能損失為代價提供更准確的結果。默認值是 false

每個索引的選項

name
(string) 索引名稱。此值直接映射到頁面參數。Hugo 支持字符串值(示例中的 author)和列表(tagskeywords 等)以及時間和日期對象。
type
(string) basicfragments 之一。默認值是 basic
applyFilter
(string) 將特定於 type 的過濾器應用於搜索結果。目前僅用於 fragments 類型。
weight
(int) 整數權重,表示此參數相對於其他參數的重要性。它可以是 0,這具有關閉此索引的效果,甚至是負數。測試不同的值以查看最適合您的內容。默認值是 0
cardinalityThreshold
(int) 如果在 1100 之間,這是一個百分比。所有在超過此百分比文檔中使用的關鍵字都將被移除。例如,將此設置為 60 將移除在索引中超過 60% 文檔中使用的所有關鍵字。如果為 0,則不從索引中移除任何關鍵字。默認值是 0
pattern
(string) 目前這僅與日期相關。當列出相關內容時,我們可能希望列出時間上也接近的內容。將 “2006”(日期索引的默認值)設置為日期索引的模式將為同年發布的頁面增加權重。對於更繁忙的博客,“200601”(年和月)可能是更好的默認值。
toLower
(bool) 是否將索引和查詢中的關鍵字轉換為小寫。這可能會以更輕微的性能損失為代價提供更准確的結果。默認值是 false

示例

假設我們正在構建一個書評網站。我們的主要內容將是書評,我們將使用流派和作者作為分類法。當有人查看書評時,我們希望基於共享的作者和流派顯示一個簡短的相關評論列表。

創建內容:

content/
└── book-reviews/
    ├── book-review-1.md
    ├── book-review-2.md
    ├── book-review-3.md
    ├── book-review-4.md
    └── book-review-5.md

配置分類法:

taxonomies:
  author: authors
  genre: genres
[taxonomies]
  author = 'authors'
  genre = 'genres'
{
   "taxonomies": {
      "author": "authors",
      "genre": "genres"
   }
}

配置相關內容識別:

related:
  includeNewer: true
  indices:
  - name: authors
    weight: 2
  - name: genres
    weight: 1
  threshold: 80
  toLower: true
[related]
  includeNewer = true
  threshold = 80
  toLower = true
  [[related.indices]]
    name = 'authors'
    weight = 2
  [[related.indices]]
    name = 'genres'
    weight = 1
{
   "related": {
      "includeNewer": true,
      "indices": [
         {
            "name": "authors",
            "weight": 2
         },
         {
            "name": "genres",
            "weight": 1
         }
      ],
      "threshold": 80,
      "toLower": true
   }
}

我們配置了 authors 索引權重為 2genres 索引權重為 1。這意味著 Hugo 優先將共享 authors 的重要性視為共享 genres 的兩倍。

然後使用這樣的 局部 模板渲染 5 個相關評論的列表:

layouts/_partials/related.html
{{ with site.RegularPages.Related . | first 5 }}
  <p>相關內容:</p>
  <ul>
    {{ range . }}
      <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
    {{ end }}
  </ul>
{{ end }}

Last updated: January 1, 0001
Improve this page