[SOLVED] Extra space after shortcode output

When using a shortcode in a Markdown file, a space is automatically inserted after the shortcode output in the rendered HTML output.

The most basic example is a shortcode that is defined simply as “some text”. Calling the shortcode with a period afterwards in the Markdown file — {% shortcode %}. — results in “some text .” in the HTML output (note the space between the shortcode output and the subsequent period).

Because it appears that Hugo doesn’t support using templates directly in the Markdown files (unlike Liquid templates in Jekyll), I planned on using shortcodes to include site parameters and apply conditional logic in the Markdown content.
In addition, I want to use shortcodes to apply custom text-formatting styles such as “func” or “filename”.
These implementations seem to work well except for the extra space issue, which is critical.

This should help you: https://gohugo.io/templates/go-templates/#whitespace

1 Like

Thanks, that did the trick!
(I had actually read this Hugo doc topic at some stage, but forgot about it … .)

Using {{- $.Site.Params.PARAMNAME -}}, for example, in the shortcode, or adding {{- "" -}} at the end of shortcodes that don’t end in a template call, worked well to trim the extra space in the output.

1 Like

Perfect.

I just wanted to emphasize the second part of this answer as it helped.

For paired shortcodes just add {{- "" -}} to the end.