Consistent behavior of markdownify for single/multiple paragaphs


#1

Hi!

Is there a good/recommended way to force consistent behavior (w.r.t. wrapping in <p>...</p> tags) of markdownify, independent of whether its called on a single paragraph or multiple paragraphs of input?

Right now (in v0.29) single paragraphs are not enclosed in <p>...</p>, while if there are multiple paragraphs each of them is. I’m open to any suggestions on how to deal with this when writing shortcodes not knowing whether to expect one or multiple paragraphs.

I read a couple of posts/discussions about this behavior, but didn’t find a solution.

Thanks!


#2

I agree. I’d prefer that paragraphs are always enclosed in <p> tags for consistency.

(I don’t know of a solution, sorry.)


#3

I suggested we add a blocks keyword or something, but I was voted down; so the current behaviour is that we keep the Ps if more than one. I mainly use this for titles etc. so that is what I want.

We should improve on this, but you could to something like this (not tested):

{{ $markdown := "Hello **world**" | markdownify }}
{{ $hasP := strings.Contains $markdown "<p>" }}
{{ if not $hasP }}<p>{{ end }}
{{ $markdown }}
{{ if not $hasP }}</p>{{ end }}

If you pull the above into a partial you could reuse it.


#4

I’ts a shame. You were right in my opinion. It was a tight vote and democracy can be a *****. I missed the voting. What seem obvious to me is that there must be a way to handle this situation without workarounds.

Talking about the workaround, testing only for a paragraph presence is not enough in some edge cases. What about if the provided markdown is something like this:

# Title

| col | col |
| --- | --- |
| 1   | 2   |

No paragraph, so it will be wrapped in a p tag. A more robust solution would be to use findRE, testing for more tags (using ‘^<(p|h[0-9]+|…)>’).

I still think this would fit better in your “blocks” functionality. I would just suggest something more self explanatory name, like blockify (if I remember well you throw this name in the discussion) or forceBlock (like “add a block, a paragraph, if not already wrapped”).

This function would be useful in more cases, not only for markdown, but for others parser or even for raw HTML. It’s a way to enforce a constraint, to not break a layout.