it-gro
January 13, 2018, 2:13pm
1
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
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:
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:
opened 03:03PM - 29 Nov 15 UTC
closed 07:15AM - 17 Aug 18 UTC
Keep
Outdated
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?
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 .
1 Like
it-gro
January 13, 2018, 4:56pm
3
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!
3 Likes
Mario
June 16, 2021, 12:15pm
4
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
Hi @Mario , please create new a new topic describing the details of your problem/question. See 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.
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.