Hugo

Hugo post summary

Hi friends,

I am trying to customize the summary that is displayed for my blog posts. It keeps using the first some number of characters. But, I want to use an image or some small html instead. I tried putting html inside summary: "..." in the article front matter, but this did nothing.

Any suggestions?
Leo

@Ilya_Nemtsev content summaries are automatically generated and accessed through the page .Summary variable. See https://gohugo.io/content-management/summaries/.
{{ .Summary | safeHTML }} should do the trick. no summary frontmatter is necessary.

I updated my template and that did the trick!
Thank you @bmackinney

I use sometimes

<p>{{ .Summary | plainify | chomp }}</p>

bc .Summary breaks HTML elements (some time ago - not tested again)

I’ve tried this but am still having some issues (sorry to post here - if I’m hijacking this thread please feel free to break out my post to a new topic).

If I use the automatically generated {{ .Summary }} all of the HTML is stripped from the output.

Is there any way to retain the HTML associated with the file being summarised?

For example, with a post markdown file with content like this at the beginning:

## First header

This is a first sentence _for example_.

This is a second sentence, new paragraph.

If I use the automatic {{ .Summary }} for this page, it renders as:

First header This is a first sentence for example. This is a second sentence, new paragraph.

(note: this also happens with {{ .Summary | safeHTML }}.

What I would expect to render is the HTML also:

<h2>First header</h2>
<p>This is a first sentence for example.</p>
<p>This is a second sentence, new paragraph.</p>

As a small work-around I’m using: {{ .Content | safeHTML | truncate 500 }}.
Where I truncate the full post by N characters (but this is not as nice as the autosummary - sometimes the break is in the middle of a sentence with ellipses).

related:
https://discourse.gohugo.io/t/summary-strips-all-html-tags-regardless-of-length/2151