# Shortcodes and \</p\> tags (2)

**URL:** <https://discourse.gohugo.io/t/shortcodes-and-p-tags-2/9987>\
**Category:** support\
**Created:** [January 13, 2018, 2:13pm UTC](https://discourse.gohugo.io/t/shortcodes-and-p-tags-2/9987 "2018-01-13T14:13:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![it-gro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/it-gro/32/4767_2.png) [@it-gro](https://discourse.gohugo.io/u/it-gro)\
**Post date:** [January 13, 2018, 2:13pm UTC](https://discourse.gohugo.io/t/shortcodes-and-p-tags-2/9987/1 "2018-01-13T14:13:13Z")

</div>

[https://validator.w3.org](https://validator.w3.org) shows errors like  
`No p element in scope but a p end tag seen.`  
on some of my pages

I’ve created a test site:  
[https://github.com/it-gro/HugoTestingShortcodeIllegalP](https://github.com/it-gro/HugoTestingShortcodeIllegalP)

shortcode is just a placeholder dummy (with a div)  
`<div class="test" >test</div>`

I see a nonconform `</p>` in the html on:

```
{{< dummy >}}
{{< dummy >}}

```

or  
some text  
{{\< dummy \>}}

or  
{{\< dummy \>}}  
more text

HTML look like e.g. this:

```
<p><div class="test" >test</div>

<div class="test" >test</div>
</p>

```

There is no issue if the shortcode is  
`<span class="test" >test</span>`

So currently the user of a shortcode has to know if the shortcode is in a block or not.

Something like this was reported a long time ago:

> [@Shortcodes and \<p\> tags](https://discourse.gohugo.io/t/shortcodes-and-p-tags/2164):
>
> After upgrading to hugo 1.5, I realised I was relying on the earlier behaviour that (perhaps unsemantically) wrapped my rendered shortcodes with \<p\> tags. Now I’m running into some inconsistent behaviour that I hope someone could help me explain. I have content that looks like the following: [snipped paragraphs that don't seem to affect behavior] {{\< figure src="/img/research/cardiac-mechanics/orthotropic-passive-stress.png" title="The orthotropic response of the myocardium model demonstrated…

and there is a pending issue:

> <https://github.com/gohugoio/hugo/issues/1642>
>
> Previous related issue 'Shortcode output wrapped in \`\<p\>\` tags' #1148 which was… closed with \[004fcdd\](https://github.com/spf13/hugo/commit/004fcddc806d5a05a6d1a908f6ef902ef76aba06).
> \#### Consistent incorrect wrapping of shortcode output under certain conditions
> 
> Reported in discuss forum \[here\](https://discuss.gohugo.io/t/shortcodes-and-p-tags/2164).
> 
> Salient points:
> 
> 1) If there is just one shortcode, it doesn't get wrapped in \`\<p\>\` tags
> 
> 2) If there are multiple shortcodes, the first doesn't get wrapped in \`\<p\>\` tags (probably related to above)
> 
> 3) In order to ensure no shortcodes are wrapped in \`\<p\>\` tags, the final shortcode must be followed by a number of distinct \`\<p\>\` tag wrapped lines proportionate to the number of shortcodes in the document.
> 
> By that I mean if you have say 6 shortcodes one after the other, you need 16 lines following them in order for none to be wrapped in \`\<p\>\` tags. If you have 5 shortcodes one after the other you need 12 lines.
> 
> 4) 'Lines following shortcode' don't have to come at the end of the document, if you have a series of shortcodes it seems the number of interceeding \`\<p\>\` tag wrapped lines are cumulatively ranked towards each consecutive shortcode so if you had 30 lines interspaced between shortcodes throughout the document, as long as each shortcode has at least 5 proceeding lines each, they'll all be spared wrapping.
> 
> 5) Related to point 4, the \`\<p\>\` tags that get wrapped around shortcodes incorrectly also count towards the number of \`\<p\>\` tag wrapped lines needed to follow preceeding shortcodes - so enough shortcodes in a row and you seem to be about to start unwrapping some of the 'first' few in the list. 
> 
> 6) Length of shortcode title attribute also affects whether output wrapped in \`\<p\>\` tags though this has not been investigated in detail
> 
> NOTE - I may have miss counted the exact number of lines in between shortcodes, my eyes were actually bleeding...

As far as I know the closing `<p>` tag is **optional**. So I’m OK with the `<p>` - but not with the `</p>` tag.

I would like to know:  
Look’s like a “block” shortcode has to be used with a preceding and following empty line in the content files.  
Is there anything I can do on the **shortcode side** to fix my pages?

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [January 13, 2018, 4:18pm UTC](https://discourse.gohugo.io/t/shortcodes-and-p-tags-2/9987/2 "2018-01-13T16:18:11Z")

</div>

Not on the shortcode side. But you can do a simple replace func checking for those stray p closing tags in your .Content and remove them.

If you need something fancier see [this](https://discourse.gohugo.io/t/how-to-have-urlize-behave-like-toc-header-id/9818/6?u=onedrawingperday).

---

<div class="post-metadata">

**Author:** ![it-gro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/it-gro/32/4767_2.png) [@it-gro](https://discourse.gohugo.io/u/it-gro)\
**Post date:** [January 13, 2018, 4:56pm UTC](https://discourse.gohugo.io/t/shortcodes-and-p-tags-2/9987/3 "2018-01-13T16:56:27Z")

</div>

Hej - cool - thx for pointing me into the right direction!

I did:

```
{{ .Content | replaceRE "<p><div" "<div" | replaceRE "(?s:</div>\n?</p>)" "</div>" | safeHTML }}

```

`https://github.com/it-gro/HugoTestingShortcodeIllegalP/blob/master/layouts/_default/single.html`

This fixed the first two test cases. Remaining

```
{{< dummy >}}
more text

```

=\>

```
<div class="test" >test</div>

more text</p>

```

I’ll do some more tests with real content.  
Thx again!

---

<div class="post-metadata">

**Author:** ![Mario](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Mario](https://discourse.gohugo.io/u/Mario)\
**Post date:** [June 16, 2021, 12:15pm UTC](https://discourse.gohugo.io/t/shortcodes-and-p-tags-2/9987/4 "2021-06-16T12:15:22Z")

</div>

Hi, can we reopen that?

A pre-deployment cleaning process is error-prone, we need to address that behaviour in Hugo directly to turn off `<p>`-wrapping in MD for certain pages, e.g. landing pages.

thanks

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [June 16, 2021, 1:44pm UTC](https://discourse.gohugo.io/t/shortcodes-and-p-tags-2/9987/5 "2021-06-16T13:44:23Z")

</div>

Hi @Mario, please create new a new topic describing the details of your problem/question. See [https://discourse.gohugo.io/t/requesting-help/9132](https://discourse.gohugo.io/t/requesting-help/9132).

> **N.B.** We discourage reviving old topics because people who participated in those discussions of _yesteryear_ are usually not here anymore and they rarely reply to questions. It is best to open a new topic rather than bumping an old one.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [June 16, 2021, 1:44pm UTC](https://discourse.gohugo.io/t/shortcodes-and-p-tags-2/9987/6 "2021-06-16T13:44:28Z")

</div>



---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [June 16, 2021, 7:48pm UTC](https://discourse.gohugo.io/t/shortcodes-and-p-tags-2/9987/7 "2021-06-16T19:48:31Z")

</div>

In addition to what @jmooring said above I would like to add that this topic is from 2018, back then Hugo used a different default Markdown processor than the one that is used currently.

Therefore this topic and the related GitHub issue are outdated.
