相关内容
Hugo 使用一组因素根据前置 matter 参数识别页面的相关内容。这可以调整到所需的索引和参数集,或者保留为 Hugo 的默认 相关内容配置 。
列出相关内容
列出最多 5 个相关页面(共享相同的 日期 或 关键词 参数)就像在模板中包含类似以下部分一样简单:
Related
方法接受一个参数,该参数可以是 Page
或选项映射。选项映射具有以下选项:
- indices
- (
slice
) 要在其内搜索的索引。 - document
- (
page
) 要为其查找相关内容的页面。指定选项映射时需要此项。 - namedSlices
- (
slice
) 要搜索的关键词,表示为使用keyVals
函数的KeyValues
片段。 - fragments
- (
slice
) 用于配置为“fragments”类型的索引的特殊关键词列表。这将匹配文档的 fragment 标识符。
一个使用上述所有选项的虚构示例:
{{ $page := . }}
{{ $opts := dict
"indices" (slice "tags" "keywords")
"document" $page
"namedSlices" (slice (keyVals "tags" "hugo" "rocks") (keyVals "date" $page.Date))
"fragments" (slice "heading-1" "heading-2")
}}
在相关内容中索引内容标题
New in v0.111.0Hugo 可以索引您内容中的标题,并使用它来查找相关内容。您可以通过向 related
配置添加类型为 fragments
的索引来启用此功能:
hugo.
related:
includeNewer: true
indices:
- applyFilter: true
name: fragmentrefs
type: fragments
weight: 80
threshold: 20
toLower: false
[related]
includeNewer = true
threshold = 20
toLower = false
[[related.indices]]
applyFilter = true
name = 'fragmentrefs'
type = 'fragments'
weight = 80
{
"related": {
"includeNewer": true,
"indices": [
{
"applyFilter": true,
"name": "fragmentrefs",
"type": "fragments",
"weight": 80
}
],
"threshold": 20,
"toLower": false
}
}
name
映射到可选的前置 matter 片段属性,可用于从页面级别链接到片段/标题级别。- 如果启用
applyFilter
,则结果中每个页面上的.HeadingsFiltered
将反映已过滤的标题。如果您想在相关内容列表中显示标题,这将非常有用:
{{ $related := .Site.RegularPages.Related . | first 5 }}
{{ with $related }}
<h2>参见</h2>
<ul>
{{ range $i, $p := . }}
<li>
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ with .HeadingsFiltered }}
<ul>
{{ range . }}
{{ $link := printf "%s#%s" $p.RelPermalink .ID | safeURL }}
<li>
<a href="{{ $link }}">{{ .Title }}</a>
</li>
{{ end }}
</ul>
{{ end }}
</li>
{{ end }}
</ul>
{{ end }}
配置相关内容
Hugo 提供了相关内容的合理默认配置,但您可以在全局或语言级别(如果需要)微调此配置。
默认配置
如果项目中未设置任何 related
配置,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
}
}
自定义配置应使用相同的语法设置。
顶级配置选项
- threshold
- (
int
) 0-100 之间的值。较低的值将给出更多匹配项,但可能不太相关。 - includeNewer
- (
bool
) 设置为true
可在相关内容列表中包含 比当前页面更新的页面 。这意味着随着新相关内容的添加,旧帖子的输出可能会发生变化。 - toLower
- (
bool
) 设置为true
可将索引和查询中的关键词都转换为小写。这可能会以略微降低性能的代价获得更准确的结果。请注意,这也可以针对每个索引进行设置。
每个索引的配置选项
- name
- (
string
) 索引名称。此值直接映射到页面参数。Hugo 支持字符串值(示例中的author
)和列表(tags
、keywords
等)以及时间和日期对象。 - type New in v0.111.0
- (
string
)basic
(默认)或fragments
之一。 - applyFilter New in v0.111.0
- (
string
) 对搜索结果应用特定于type
的过滤器。目前这仅用于fragments
类型。 - weight
- (
int
) 一个整数权重,指示此参数相对于其他参数的 重要性 。它可以是0
,这将关闭此索引,甚至可以是负数。使用不同的值进行测试,以查看最适合您的内容的值。 - cardinalityThreshold New in v0.111.0
- (
int
) 如果在 1 到 100 之间,则这是一个百分比。所有在超过此百分比的文档中使用的关键词都将被删除。例如,将其设置为60
将删除在索引中超过 60% 的文档中使用的所有关键词。如果为0
,则不会从索引中删除任何关键词。默认为0
。 - pattern
- (
string
) 这目前仅与日期相关。列出相关内容时,我们可能希望列出时间上也接近的内容。将“2006”(日期索引的默认值)设置为日期索引的模式将增加在同一年发布的页面的权重。对于更繁忙的博客,“200601”(年份和月份)可能是更好的默认值。 - toLower
- (
bool
) 见上文。
性能注意事项
快速是 Hugo 的中间名 ,如果没有它非常快,我们就不会发布此功能。
此功能长期以来一直存在于积压中,并被许多人请求。最近,来自此 Twitter 线程的开发得到了推动:
Holy smokes! Build time dropped to 1.2 seconds!
— Scott S. Lowe (@scott_lowe) August 18, 2017
Scott S. Lowe 删除了使用 intersect
模板函数在标签上构建的“相关内容”部分,并且在他的 1700 个内容页面大小的博客上,构建时间从 30 秒下降到不到 2 秒。
他现在应该能够添加改进版本的“相关内容”部分,而不会放弃快速的实时重新加载。但值得注意的是:
- 如果您不使用任何
Related
方法,则不会使用“相关内容”功能,性能将与以前相同。 - 调用
.RegularPages.Related
等将创建一个倒排索引(有时也称为发布列表),该索引将被重用于同一页面集合中的任何查找。例如,除了调用.Pages.Related
之外,这样做将按预期工作,但会创建一个额外的倒排索引。这仍然应该非常快,但值得牢记,尤其对于更大的网站而言。