I have been looking around to see if anyone has come across an issue I have been experiencing with the markdownify function. Apparently there are quite a number of folks; in fact there have been an issue on github and other questions like this.
That said, I have not found a conclusive solution to this problem. My experience with kramdown, the markdown engine Jekyll uses by default,
<div>
{{ someVariable | markdownify }}
</div>
always produces properly wrapped content like so
<div>
<p>... my content ...</p>
</div>
and never something as disturbing as this
<div>
"... some unwrapped text ..."
</div>
The latter situation is quite infuriating especially because it leads to design inconsistencies. Normally, I would result to JavaScript to resolve issues like this. I bet it would be easier to remove unwanted paragraph tags using JavaScript than add one to orphaned text.
@mhhollomon, It’s been a while since I have had to use this technique. Maybe I found a solution without even having to think about it after all. Is your code publicly accessible?
For my case, I am trying to handle the .Summary correctly.
If it comes from the auto summary, then it is guaranteed to not have <p>.
If it comes from the manual summary then it is pretty much guaranteed to have<p>.
If it comes from the page header data, I want to support markdown, so use markdownify to process, it may or may not have <p>.
In truth, since I don’t know the actual origin, I just pass whatever string is in .Summary into markdownify and use the strings.Contains logic from earlier answers in this thread.
You can ensure hugo won’t trim any p element by adding a paragraph to your string, formatting it and trimming the extra paragraph, something like that: