Markdown render hooks extra space

Hi everybody.

I’m using a Markdown Render Hook to customize the links within the .md files. This is what I have.


<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if strings.HasPrefix .Destination "https" }} target="_blank" rel="noopener"{{ end }}>{{ .Text | safeHTML }}</a>

Every is going fine, except that I’m getting an extra space after the rendered link:

[Medium](, it's a web site.

It renders:

Medium , it's a web site.

I have tried with this code of “portable-hugo-links” written by @bep:

{{ $link := .Destination }}
{{ $isRemote := strings.HasPrefix $link "http" }}
{{- if not $isRemote -}}
{{ $url := urls.Parse .Destination }}
{{- if $url.Path -}}
{{ $fragment := "" }}
{{- with $url.Fragment }}{{ $fragment = printf "#%s" . }}{{ end -}}
{{- with .Page.GetPage $url.Path }}{{ $link = printf "%s%s" .RelPermalink $fragment }}{{ end }}{{ end -}}
{{- end -}}
<a href="{{ $link | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if $isRemote }} target="_blank"{{ end }}>{{ .Text | safeHTML }}</a> 

But the extra space is still appearing.

Thanks for your help.

Ok, I just tried it and it seems I only get the extra space if I have an empty line at the end of the render-link.html.

Check your editor config, looks like the editor add newline at EOF on save.

1 Like

I just have one single line of code.

Thanks for your interest.

Yes, and as @pamubay pointed out, it ends with a newline character.

If your file did not end with a newline character, the following command would display the > character:

tail -c 1 layouts/_default/_markup/render-link.html

Instead, the above command produced a blank line, indicating that your file ends with a newline character.

Add this at the end of your file to consume the newline:

{{- /* This comment removes trailing newlines. */ -}}

Thanks for your reply @jmooring, it was very clear.

Also, as @sephore said, I look up into my Atom text editor config and I found the Whitespace package, then I disabled it in order to save the file without the newline character.

Of course, as you pointed out, using the comment {{- /* This comment removes trailing newlines. */ -}} touch the configuration of the text editor is not necessary.

Now, the trailing whitespace at the rendered links and other shortcodes that I have (for example a shortcode for abbr html tag) works as expected.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.