Search code examples
hugo

Hugo: End content summary at first paragraph break


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?


Solution

  • 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 }}