雨果:第一段中断时结束内容摘要

发布于 2025-02-13 10:13:01 字数 387 浏览 1 评论 0原文

我正在尝试从Jekyll迁移博客,我想将摘录/内容摘要保持不变。也就是说,我想在第一段中断时自动结束摘要。

I should be included in the content summary.
Still in summary.

This is no longer part of summary

文档似乎表明Hugo仅支持自动摘要按长度

是否有某种方法可以自动打破第一段中断的内容摘要?

I'm trying to migrate a blog from Jekyll, and I'd like to keep my excerpts/content summaries the same. That is, I'd like to automatically end the summary at the first paragraph break.

I should be included in the content summary.
Still in summary.

This is no longer part of summary

The docs seem to indicate that Hugo only supports automatic summaries by length.

Is there some way to automatically break the content summary on the first paragraph break?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假扮的天使 2025-02-20 10:13:01

这就是我的工作方式,基于Rogelio的答案:

{{ $summary := index (split .Content "</p>") 0 }}

上面的代码通过关闭p标记将整个文章的内容分配,检索结果数组的第一个元素,然后将其存储在<<代码> $摘要变量。

该解决方案的关键在于:

  1. 使用整个内容而不是摘要,因此无论多长时间,您的第一段都将保留。
  2. 通过关闭&lt;/p&gt;不是打开&lt; p&gt;标签来分裂内容。

此时,$汇总包含以下代码:

<p>
I should be included in the content summary.
Still in summary.

之后,我们可以输出摘要。只需确保使用plainify删除HTML,以摆脱未闭合的&lt; p&gt;标签。

{{ $summary | plainify }}

This is how I got it working, based on Rogelio's answer:

{{ $summary := index (split .Content "</p>") 0 }}

The code above splits the content of the whole article by closing p tag, retrieves the first element of the resulting array, and stores it in the $summary variable.

The key to this solution resides in:

  1. Using the whole content instead of the summary, so your first paragraph will be kept no matter how long it is.
  2. Splitting the content by closing </p> and not the opening <p> tag.

At this point, $summary contains this code:

<p>
I should be included in the content summary.
Still in summary.

After that, we can output the summary. Just make sure to remove HTML using with plainify, to get rid of the unclosed <p> tag.

{{ $summary | plainify }}
倾城月光淡如水﹏ 2025-02-20 10:13:01

做一个短代码或部分 - 取决于您使用的位置。
使用.summary变量
使用拆分功能。

PSUEDO代码:

{{ split .Summary "<p>" }}

它将为您提供一个映射/切片/阵列的值。

因此,更多的psuedo代码:

{{ index (split .Summary "<p>") 0 }}

等等。

Make a shortcode or partial - depending on where you are using it.
Use the .Summary variable
Use the Split function.

Psuedo-code:

{{ split .Summary "<p>" }}

Which will give you a map/slice/array of values.

So more psuedo code:

{{ index (split .Summary "<p>") 0 }}

etc.

梦罢 2025-02-20 10:13:01

不,雨果比总结摘要更多。

可以通过多种方式指定摘要,并且在决定要返回的文本时,请理解选择雨果的顺序是有用的。如下:

  • 如果文章中存在A 摘要分隔线,则
  • 如果有摘要 将提供变量的值
  • 根据本质摘要方法,

文章中的变量, 。因此,例如,如果您的文章在其前提方面具有摘要变量,并且摘要分隔线将使用手动摘要方法。

确保两者之间没有空白。

<!--more-->

或者

summary: Here your first paragraph, but be careful, you 
         have to copy it so that it appears twice in the
         file. IMO this is only advantageous if you want to 
         make a difference to the text in the summary. ....

No, Hugo supports more than summaries by length.

There are multiple ways in which a summary can be specified and it is useful to understand the order of selection Hugo follows when deciding on the text to be returned by .Summary. It is as follows:

  • If there is a summary divider present in the article the text up to the divider will be provided as per the manual summary split method
  • If there is a summary variable in the article front matter the value of the variable will be provided as per the front matter summary method
  • The text at the start of the article will be provided as per the automatic summary split method

Hugo uses the first of the above steps that returns text. So if, for example, your article has both summary variable in its front matter and a summary divider Hugo will use the manual summary split method.

Make sure there are no blanks in between.

<!--more-->

or

summary: Here your first paragraph, but be careful, you 
         have to copy it so that it appears twice in the
         file. IMO this is only advantageous if you want to 
         make a difference to the text in the summary. ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文