getzola / zola

A fast static site generator in a single binary with everything built-in. https://www.getzola.org
https://www.getzola.org
MIT License
13.45k stars 944 forks source link

Shortcode markdown filter results in a superfluous newline at end #2433

Open vilhelmgray opened 7 months ago

vilhelmgray commented 7 months ago

Zola version: 0.18.0

I have the following shortcode as templates/shortcodes/blog_example.html:

<div>
{{ body | markdown | safe }}
</div>

I use the shortcode like this:

{% blog_example() %}
foobar
{% end %}

I see the following HTML output generated:

<div>
<p>foobar</p>

</div>

A superfluous newline was introduced at the end of the markdown filter output. If I remove the markdown filter and leave just body, no extra newline is generated.

Is this extra newline behavior intentional? I'm not sure if this is related to https://github.com/getzola/zola/issues/2234.

Keats commented 7 months ago

Hmm not sure why that would happen but that's not intentional

SumDonkuS commented 6 months ago

Looking at components/markdown/src/shortcode/markdown.rs markdown_to_html method, the call to cmark::push_html the event stack is [Start(Paragraph), Text(Boxed("foobar")), End(Paragraph)]. Once rendered into the html variable it comes out as "<p>foobar</p>\n".

Looking a little further, it looks like the extra line break is with HtmlWriter::end_tag of cmark here.

            Tag::Paragraph => {
                self.write("</p>\n")?;
            }
NotAFedoraUser commented 4 months ago

I have the same problem with some of my shortcodes, the "solution" I ended up doing was just putting the entire shortcode code in one single line and the newline issue didn't occur, at least for me.

On version 0.18.0 of Zola.