Create description from summary

Is there a way to get the first n characters of a post with no broken word at the end and an ellipsis added if the last character is not a sentencemark? I am trying to create a dummy-proof way to fill in my description tag which should be something along the line of n characters, which might not be the number of words or characters that I want to show in a summary for a post. I would solve this by cutting out the n characters from whatever .Summary (or .Plain) gives me and then try to use a regexp to cut off everything after the last space and add the ellipsis if the trimmed string does not end on a .!? character. Is there an easier way to accomplish this? Overthinking?

Personally I use this technique

Also see strings.Truncate | Hugo

1 Like

If you were truncating the first line to 60 characters…

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus
123456789012345678901234567890123456789012345678901234567890

Would you want this (period replaced by ellipsis)?

Lorem ipsum dolor sit amet, consectetur adipiscing elit...

Or this (period followed by ellipsis)?

Lorem ipsum dolor sit amet, consectetur adipiscing elit....

I like the first one because a period followed by an ellipsis character just doesn’t look right to me. Edge cases…

Similarly…

Lorem ipsum dolor sit amet, consectetur adipiscing elit...    OR
Lorem ipsum dolor sit amet, consectetur adipiscing elit?...

Lorem ipsum dolor sit amet, consectetur adipiscing elit...    OR
Lorem ipsum dolor sit amet, consectetur adipiscing elit!...
Also see [truncate | Hugo ](https://gohugo.io/functions/truncate/#readout)

This looks good. I’ll play around with it.

Lorem ipsum dolor sit amet, consectetur adipiscing elit... 
Lorem ipsum dolor sit amet, consectetur adipiscing elit!

If there is a sentence mark (.?!), then nothing, if it’s in the middle of a sentence then add an ellipsis.

{{ partial "truncate" (dict "input" .Summary "maxLength" 50) }}
layouts/partials/truncate.html
{{ $input := .input | plainify | replaceRE `\r` "" | replaceRE `\n` " " | replaceRE `\s{2,}` " " }}
{{ $maxLength := .maxLength | int }}
{{ $endMarks := slice "." "?" "!" }}

{{ $regex := printf `(?s)^.{0,%d}\W` (sub $maxLength 1) }}
{{ $truncated := index (findRE $regex $input) 0 | replaceRE `\s+$` "" }}
{{ if not (in $endMarks (substr $truncated -1)) }}
  {{ $truncated = replaceRE `\W+$` "" $truncated }}
  {{ $truncated = print $truncated "…" }}
{{ end }}

{{ return $truncated }}

Try it:

git clone --single-branch -b hugo-forum-topic-36676 https://github.com/jmooring/hugo-testing hugo-forum-topic-36676
cd hugo-forum-topic-36676
hugo server
12 Likes

Perfect, exactly what I tried to achieve :slight_smile: Thank you @jmooring!

You’re welcome! Good luck with a CJK implementation…

:joy: oh yeah… I wonder if there is a way to know if a string is in a specific utf-8 range. My brain says ord but I can’t find something like it. But that will have to wait until a friendly CJK user is asking for it.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.