I’m working on a fun little snippet of shortcode (theme/module maybe?) that will extend the functionality of code blocks and hilighting to allow for line numbers, github links, go playground links and so on. One of the issues that confronted me early on was the addition of newlines to my code, which was annoying because it threw off the line numbers.
The solution to this ended up being pretty simple: trim. I just pass .Inner to trim with a second argument of “\n”, and all leading and trailing newlines are removed!
{{ with .Get "name" }}<b>{{.}}</b>{{ end }}
<pre><code{{ with .Get "lang" }} class="language-{{.}}" {{ end }}>{{ trim .Inner "\n" }}
</code></pre>