hugo: Add "block" option/action to markdownify

As @robsonsobral commented in the original issue #1025 that added this “functionality”, this is really non-standard, confusing behaviour. Why was it added in? More importantly, can it be removed, or put behind a config option?

For context, refer to this commit be627fa718d23278aeb53da5651a8a4a1be14c27. Hugo manually strips the leading and trailing paragraph tags, thereby rendering the markdownify function useless for any string that contains block level markdown.

This can cause major bugs! For example, if your yaml frontmatter looks like this:

somekey: |
  This is a **valid** markdown paragraph.

  This is another **valid** markdown paragraph. And it's *still valid yaml!*
someotherkey: 2

markdownify will output the following, incorrect HTML, where the opening tag for the first paragraph, and closing tag for the last paragraph are missing!

This is a <strong>valid</strong> markdown paragraph.</p>
<p>This is another <strong>valid</strong> markdown paragraph. And it's <em>still valid yaml!</em>

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 8
  • Comments: 20 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I am not sure if I understand you correctly @noonat, but @bep posted something similar somewhere else, and I used that in order to always wrap my text in either a single <p> tag or multiple:

{{ $markdown := .intro | markdownify }}

{{ if not ( strings.Contains $markdown "<p>" ) }}
    <p>{{ $markdown }}</p>
{{ else }}
    {{ $markdown }}
{{ end }}

Just replace .intro with your own.

Perhaps markdownifyBlock & markdownifyInline functions? As mentioned, I see the use-case for inline markdown styles in headers, but calling the function markdown and then producing non-standard output is highly confusing.

and it is easier to add p tags than it is to remove it from a template.

It’s actually a lot harder. Especially when you have non-<p> elements that begin or end the string.

I’m also going to chip in, as it feels like a very odd decision to strip the <p> (or simply don’t add it) when the input for the Hugo pipe markdownify.

"content": {
  "text1": "Liquorice cake cake jelly jelly beans. Candy oat cake oat cake oat cake sesame snaps. Croissant cotton candy carrot cake cake brownie wafer chupa chups chocolate.",
  "text2": "Liquorice cake cake jelly jelly beans.\n\nCandy oat cake oat cake oat cake sesame snaps. Croissant cotton candy carrot cake cake brownie wafer chupa chups chocolate."
}

{{ with .content.text1 }}
    <div class="c-content">
        {{ . | markdownify  }}
    </div>
{{ end }}

{{ with .content.text2 }}
    <div class="c-content">
        {{ . | markdownify  }}
    </div>
{{ end }}

Currently:

<div class="c-content">
    Liquorice cake cake jelly jelly beans. Candy oat cake oat cake oat cake sesame snaps. Croissant cotton candy carrot cake cake brownie wafer chupa chups chocolate.
</div>
<div class="c-content">
    <p>Liquorice cake cake jelly jelly beans.</p>
    <p>Candy oat cake oat cake oat cake sesame snaps. Croissant cotton candy carrot cake cake brownie wafer chupa chups chocolate.</p>
</div>

Expected:

<div class="c-content">
    <p>Liquorice cake cake jelly jelly beans. Candy oat cake oat cake oat cake sesame snaps. Croissant cotton candy carrot cake cake brownie wafer chupa chups chocolate.</p>
</div>
<div class="c-content">
    <p>Liquorice cake cake jelly jelly beans.</p>
    <p>Candy oat cake oat cake oat cake sesame snaps. Croissant cotton candy carrot cake cake brownie wafer chupa chups chocolate.</p>
</div>

I hope it is something that will reconsider for you to reopen the issue, and implement a fix or add an option to enable the expected behavior 😃

Dropping some links aswell: https://discourse.gohugo.io/t/markdownify-dosnt-add-paragraph-html-tag-on-string-input/18592 https://discourse.gohugo.io/t/markdownify-and-paragraphs/15883 https://discourse.gohugo.io/t/first-paragraph-with-markdownify-not-being-wrapped-with-p-tag/6259/2

Here’s an improved version of @imjasonmiller’s code - it will add missing <p> tags if there’s no <p> or <hx> tags but will ignore other tags such as <span> or <em>:

(Copying my solution from the forum, I hope that’s OK.)

{{- $markdown := .Inner | markdownify -}}

{{ if not ( findRE "<[h|p][^>]*>" $markdown ) }}
    <p>{{ $markdown }}</p>
{{ else }}
    {{ $markdown }}
{{ end }}