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