Honor newlines when overlaying text on image

Hi,

I have a snippet params in some of my blog pages, with new lines like so

snippet: |
   What’s happening in China is really interesting. If we trust their official figures and estimates, the economy was driven by 3 things

   1. Real estate (who’s crashing hard)
   2. Consumer spending (who’s in decline, as their population is not growing and aging)
   3. Exports (who are threatened by India and South East Asia)

Then in a partial I have the following to render the text on top of an image

  {{ if .Page.Params.Snippet }}
  {{ $img = $img.Filter (images.Text .Page.Params.Snippet (dict
    "color" "#ffffff"
    "size" 32
    "linespacing" 2
    "x" 40
    "y" 150
    "font" $mediumFont
  ))}} 
  {{ end }}

However, the new lines are not rendered. I’ve tried a couple of things but end up either having raw html or having an HTML object instead of a string passed to images.Text.

Does someone have any idea on how to approach this?

This isn’t currently possible.

We split the string into words with strings.Fields:

Fields splits the string s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an empty slice if s contains only white space.

The unicode.IsSpace function treats a newline as whitespace, so this simple approach to word wrapping eats the line breaks.

1 Like

Thanks for confirming. I thought I wasn’t being smart enough, but it’s the API.

Yeah, I’m going to log an enhancement for this. It’s a simple fix.
https://github.com/gohugoio/hugo/issues/12206

Fixed in v0.123.8.

1 Like

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