Remove empty <p>'s from Content

I’m trying to remove paragraphs from rendered content that don’t have anything in between them

I’ve had a look at Remove empty paragraph tags from string but that doesn’t seem to work.

 {{ .Content | replaceRE `<p>\s*</p>` "" | safeHTML }}

Thanks in advance.

Are you sure that the empty elements are in the content? Or could they be coming from one of the templates (outside of content)?

Thanks for your reply.

I’m wrapping like so:

<div class="richtext">
    {{ .Content | replaceRE `<p>\s*</p>` "" | safeHTML }}

And I can see there are <p></p>'s within .richtext.

Ah, Google Chrome was making it seem like something else:

<p>Lorem ipsum
<div class="image-wrapper">
   <picture class="image">
      <img src="hello.jpg"/>

I’m desperately trying to avoid wrapping the inline pictures for markdown in paragraphs, so I can style the markdown correctly.


Is there any way around this? I’m pretty sure there may be some clever regex, but I can’t figure it out.

Thanks in advance.

Are you using an image render hook?

Yep, example is below in the hook.
I did see something in the markdown docs which looked like exactly what I was looking for but it didn’t work, wrapStandAloneImageWithinParagraph: false

<div class="image-wrapper">
	<picture class="image">
        {{ partial "media/picture.html" (dict "image" (.Page.Resources.Get (.Destination | safeURL)) "alt" .Text) }}

This will remove the wrapping paragraph element from stand-alone images:

wrapStandAloneImageWithinParagraph = false

Requires v0.108.0 or later.

See the release notes for a test that you can do inside the render hook to determine if you should wrap the image in a block element, or leave as-is.


Appreciate your response an explanation, but not sure why I couldn’t get that property to work when false. Does it apply to specific use cases?

Please post your config file, and make sure you are running v0.108.0 or later.

1 Like

Will double check the version tomorrow so not to waste your time. If it’s up to date I’ll post the config. Thanks for your help so far!

Turned out I needed to add a line break after text with the proceeding image.

Thanks for your help!

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