This is what I use in quite many layout files and shortcodes:
There’s also @imjasonmiller’s solution:
{{ $markdown := .intro | markdownify }}
{{ if not ( strings.Contains $markdown "<p>" ) }}
<p>{{ $markdown }}</p>
{{ else }}
{{ $markdown }}
{{ end }}
However, both of these suffer from the same issue, which is that they work fine with a plain sentence, but will break with headings. That is, puttting the solution in a shortcode called markdownifyshortcode , and
{{ "# heading" | markdownifyshortcode }}
will output invalid HTML.
<p><h1 id="test">test</h1></p>.
Here’s a solution that should work for most scenarios - it will add missing <p> tags if there’s no <p> or <h1,2,3,...> tags but will ignore other tags such as <span> or <em>:
{{- $markdown := .Inner | markdownify -}}
{{ if not ( findRE "<[h|p][^>]*>" $markdown ) }}
<p>{{ $markdown }}</p>
{{ else }}
{{ $markdown }}
{{ end }}