内容摘要
您可以手动在 front matter 中定义内容摘要,或自动生成。手动内容摘要优先于 front matter 摘要,而 front matter 摘要优先于自动摘要。
请查看下面的 比较表 以了解每种摘要类型的特性。
手动摘要
使用 <!--more-->
分隔符指示内容摘要的结束位置。Hugo 不会渲染摘要分隔符本身。
content/sample.md
+++
title: 'Example'
date: 2024-05-26T09:10:33-07:00
+++
Thénardier was not mistaken. The man was sitting there, and letting
Cosette get somewhat rested.
<!--more-->
The inn-keeper walked round the brushwood and presented himself
abruptly to the eyes of those whom he was in search of.
当使用 Emacs Org Mode 内容格式 时,使用 # more
分隔符指示内容摘要的结束位置。
前置 matter 摘要
使用 front matter 定义独立于内容的摘要。
content/sample.md
+++
title: 'Example'
date: 2024-05-26T09:10:33-07:00
summary: 'Learn more about _Les Misérables_ by Victor Hugo.'
+++
Thénardier was not mistaken. The man was sitting there, and letting
Cosette get somewhat rested. The inn-keeper walked round the
brushwood and presented himself abruptly to the eyes of those whom
he was in search of.
自动摘要
如果您尚未手动或在 front matter 中定义摘要,Hugo 会根据您站点配置中的 summaryLength
自动定义摘要。
content/sample.md
+++
title: 'Example'
date: 2024-05-26T09:10:33-07:00
+++
Thénardier was not mistaken. The man was sitting there, and letting
Cosette get somewhat rested. The inn-keeper walked round the
brushwood and presented himself abruptly to the eyes of those whom
he was in search of.
例如,如果 summaryLength
为 10,则自动摘要将为
Thénardier was not mistaken. The man was sitting there, and letting
Cosette get somewhat rested.
请注意,summaryLength
是一个近似的单词数。
比较
每种摘要类型都有不同的特性
类型 | 优先级 | 渲染 Markdown | 渲染短代码 | 去除 HTML 标签 | 用 <p> 包装单行 |
---|---|---|---|---|---|
手动 | 1 | ✔️ | ✔️ | ❌ | ✔️ |
前置 matter | 2 | ✔️ | ❌ | ❌ | ❌ |
自动 | 3 | ✔️ | ✔️ | ✔️ | ❌ |
渲染
通过在 Page
对象上调用 Summary
方法,在模板中渲染摘要。
{{ range site.RegularPages }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
<div class="summary">
{{ .Summary }}
{{ if .Truncated }}
<a href="{{ .RelPermalink }}">More ...</a>
{{ end }}
</div>
{{ end }}