Hugo: Allow markdownify as a block for .Inner

Created on 19 May 2019  ·  4Comments  ·  Source: gohugoio/hugo

Typical construction is:

{{ .Inner | markdownify }}

The current logic is that we remove surrounding <p></p> if there is only one paragraph.

For people doing {{ .Inner }} => {{ .Inner | markdownify }} because of the changes made in 0.55, this behaviour would be subtle breaking.

I don't think we'd want this for every .Inner (that would breaking it for others), but I suggest we add a new block keyword (it has been discussed before), so you would then do:

{{ .Inner | block | markdownify }}

Also see https://github.com/gohugoio/hugo/issues/3040

Bug

Most helpful comment

I think the other way around makes more sense, e.g.

{{ .Inner | markdownify | inline }}

The default behaviour of any markdown parser is that regular text is always wrapped in a paragraph. Imho it's a more special case to want markdown "inline" somewhere.

All 4 comments

I think the other way around makes more sense, e.g.

{{ .Inner | markdownify | inline }}

The default behaviour of any markdown parser is that regular text is always wrapped in a paragraph. Imho it's a more special case to want markdown "inline" somewhere.

inner seems more obvious because the block behaviour is what you would normally expect.

If I get it correctly either inner or block would make ‘hacks’ like this obsolete (source).

{{/* Workaround markdownify inconsistency for single/multiple paragraphs */}}
{{- $raw := (markdownify .Inner | chomp) -}}
{{- $block := findRE "(?is)^<(?:address|article|aside|blockquote|canvas|dd|div|dl|dt|fieldset|figcaption|figure|footer|form|h(?:1|2|3|4|5|6)|header|hgroup|hr|li|main|nav|noscript|ol|output|p|pre|section|table|tfoot|ul|video)\\b" $raw 1 -}}

What about adding a config option about markdownify behavior? E.g.

strip <p> if one paragraph
do not strip <p>
split paragraphs by <br>

etc.? Or even make “markdownifyNoParagraph” or something similar?

Just discovered this _invented-here_ behavior of stripping <p> tags when the content is only one line. I have this line in my single.html template:

{{ .Content | markdownify }}

But when the content of an item has only one line, there is no <p> tag added, and the style of the single template breaks.

On closer inspection, I see that I am also guilty of using the no-paragraph markdownify, earlier in the same template:

<h1>{{ .Title | markdownify }}</h1>

Therefore, I'm voting for @fritzmg suggestion, moving the special paragraph-ignoring behavior-- this is an exception to the syntax published at https://www.markdownguide.org/ --to a new helper that strips paragraphs. My new template would look like this:

<h1>{{ .Title | markdownify | inline }}</h1>
{{ .Content | markdownify }}

Yes, it'll necessitate refactoring of templates built for previous versions of Hugo. But that's a side effect of the original mistake of inventing new behavior for something as ubiquitous as Markdown.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arikroc picture arikroc  ·  3Comments

chrissparksnj picture chrissparksnj  ·  3Comments

digitalcraftsman picture digitalcraftsman  ·  3Comments

marekr picture marekr  ·  3Comments

VoidingWarranties picture VoidingWarranties  ·  3Comments