Extra newline after every link due to render-link.html

Can we please have a fix for this as part of Hugo? There are so many people who are having issues with this.

It is very annoying to configure editors to not insert an empty line at the end of each file. I tried VSCode, vim, TextEdit, nano (with --nonewlines flag!), and each of them gave me the empty newline.

Edit: Even sed cannot get the job done

gsed -i '/^$/d' layouts/_default/_markup/render-link.html

Edit 2: GitHub’s editor also cannot do it, when trying to remove the last empty newline you just get an empty commit.

Have you tried using {{- and -}} template syntax to remove the whitespace?
Could you please provide your hook code if it doesn’t help?

@razon I don’t think that will solve this particular issue, which I guess could look something like this:

<a href="foo">Foo</foo>

If an editor (or Prettier) insists on adding a trailing newline to every file, I don’t see how you currently can avoid it in the above.

I would gladly add something to Hugo to fix this if someone can come up with a reasonably simple thing. I’m not sure if we can “chomp” the newline of all these hook templates, there may be people that need those.

2 Likes

I tried using

<a
  href="{{ .Destination | safeURL }}"
  {{ with .Title }}
    title="{{ . }}"
  {{ end }}{{ if strings.HasPrefix .Destination "http" }}
    target="_blank" rel="noopener"
  {{ end }}
>
  {{ if strings.HasPrefix .Destination "https://github.com/" }}
    {{ $url_parts := split (replace .Destination "https://github.com/" "") "/" }}
    {{ if eq (len $url_parts) 4 }}
      #{{ index $url_parts (sub (len  $url_parts) 1) }}
    {{ else }}
      {{ .Text | safeHTML }}
    {{ end }}
  {{ else }}
    {{ .Text | safeHTML }}
  {{ end }}
</a>
{{- /* This comment removes trailing newlines. */ -}}

But there is still a space after every link.

@bep Maybe ‘chomping’ one (empty) newline is an option?

This is just a workaround of my use case even I’ve removed the EOL.

It would be great if Hugo can handle the newline.

For future reference, if you are on macOS, you need to install GNU Nano because the default nano on macOS does not have the -L or --nonewlines option.

After

brew install nano

You need to add

export PATH="$(brew --prefix nano)/bin:$PATH"

to your ~/.zshrc so it uses the newly installed GNU Nano. Use which nano to confirm. Then you can remove the newline by editing the file with the aforementioned command line option.

Edit: OK, that still doesn’t work. :unamused:

The @razon solution is good, I tried your code with {{- or -}} everywhere, and the trick {{- "" -}} at the last line (or with an extra blank line), and it works.

3 Likes

That indeed seems to work.

Thank you.

I hope this can be simplified a bit somehow.