Using {{- .Inner -}} still adds newlines

According to this article, if you add dashes when getting a variable it should strip all new lines and white-space.

I’m currently using a custom shortcode that looks like this:

<pre><code class="language-{{- .Get 0 -}}">{{- .Inner -}}</code></pre>

If I use it like so:

{{< prism yaml >}}
highlighter: none
{{< /prism >}}

It renders output like:

<pre><code class="language-yaml">
highlighter: none
</code></pre>

Which isn’t correct because it has newlines surrounding the highlighter: none even though the dashes are supposed to remove it.

From my understanding, it should just output <pre><code class="language-yaml">highlighter: none</code></pre>.

Is it just user error on my part or something else?

I’m using Hugo version 0.39.

1 Like

I confirm this issue:

But there’s also a workaround. Try:

{{< prism yaml >}}highlighter: none{{< /prism >}}

I found that workaround as well but the issue is that whenever I have content spanning multiple lines that are surrounded in the short-codes, it is very ugly.

Do you think this is a bug within Hugo?

It’s not clear if the current behavior is intended or not. I’ll allow @bep to comment on that.

I agree that the workaround looks quite ugly.

1 Like

Any comment on this @bep?

I’m pretty confident that the shortcode parser/handler does not add anything, so I would expect the same as you in the above. But whitespace in Go templates is little bit of a mystery.

So, if you could try the same construct (if possible) in a regular template and see how that works …

package main

import (
    "os"
    "text/template"
    "fmt"
)

type Data struct {
    Content string
}

func main() {
    validData := Data{"highlight: true"}

    brokenData := Data{"\nhighlight: true"}

    tmpl, err := template.New("test").Parse("{{- .Content -}}")
    if err != nil { panic(err) }

    fmt.Printf("Doesn't contain a new line\n")
    err = tmpl.Execute(os.Stdout, validData)
    if err != nil { panic(err) }

    fmt.Printf("\nContains a new line but should be removed\n")
    err = tmpl.Execute(os.Stdout, brokenData)
    if err != nil { panic(err) }

}

If you run the above example, the second data entry (brokenData) contains a new line but Go doesn’t seem to be stripping like it is supposed to. So is the issue with Go itself or is my test wrong?

I ran the above with go v1.10.1 (linux/amd64)

Your test looks correct and reproduces the exact same scenario. I ran it myself with the same result.

I reread the spec about this:

And this looks like a bug to me.

Could you raise an issue here:

Add the test program on the issue so it is obvious what you are talking about.

Would it be best to re-open the issue I made previously on GitHub or make a new one?

Sorry, I posted the wrong link:

Here is a link to the created issue

1 Like

The above issue was closed as the dashes aren’t supposed to remove newlines from the variable, but the area surrounding it.

I’m just using this now:

<pre><code class="language-{{- .Get 0 -}}">{{ trim .Inner "\n" }}</code></pre>
1 Like