How to prevent empty paragraphs around front matter variable (<p></p>)?

In my front matter I have variables like so:

about_me: |
    *Hi* there! 
    This is an [example](/home/).

I have the following goal:

  • Include about_me into my Hugo template,
  • And put it into a paragraph (<p>) tag.

Sounds simple enough! But not so in reality. :tired_face:

Markdownify example

This works fine when the front matter variable has two paragraphs:

{{ .Params.about_me | markdownify }}
<p><em>Hi</em> there!</p> 

<p>This is an <a href="/home/">example</a>.</p>

But what if the front matter variable has a single paragraph?

about_me: |
    *Hi* there! This is an [example](/home/).
<em>Hi</em> there! This is an <a href="/home/">example</a>.

No paragraph tag! So let’s add one:

<p>{{ .Params.about_me | markdownify }}</p>

The output with a single paragraph is now:

<p><em>Hi</em> there! This is an <a href="/home/">example</a>.</p>

It works!

But what if the front matter variable has multiple paragraphs?

about_me: |
    *Hi* there! 
    This is an [example](/home/).

Now we get:

<p><p><em>Hi</em> there!</p> <p>This is an <a href="/home/">example</a>.</p> </p>

And there we have it, the erroneous <p> and </p> tag.

>> How do I prevent this from happening? What’s the 2020-approach of handling these situations?

RenderString perhaps?

Unfortunately, .RenderString suffers from the same problem.

Hugo Static Site Generator v0.69.2-EC9DCF30 windows/amd64 BuildDate: 2020-04-24T07:51:17Z

Have a look at this shortcode:

where I workaround a similar problem. I must confess I borrowed the code somewhere, but can’t remember where. So I cannot give credits where it’s due.

1 Like

Given the results from Search results for 'markdownify paragraphs' - HUGO, is there a non-2020 solution that no longer makes sense? I’m not sure what the implication is.

Thanks! I’ll study the code and see if I can rework it for my use case. :slightly_smiling_face:

The implication is to get the current best practice.

Because now we have RenderString, perhaps that is a better alternative to markdownify? Either way, Markdown processing has changed now we got Goldmark. And RenderString and markdownify are similar (but also different) so I might be using the wrong tool.