Hi everybody.
I’m using a Markdown Render Hook to customize the links within the .md files. This is what I have.
layouts/_default/_markup/render-link.html:
<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](https://medium.com), 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.
Should a text file end with a newline, or not? At first glance this may seem like a tabs vs. spaces discussion, but the POSIX standard provides the following definitions:
Text File : A file that contains characters organized into zero or more lines.
Line : A sequence of zero or more non-newline characters plus a terminating newline character.
By definition, a text file without a newline has zero lines. It seems to me that every text file with content should have at least one line, which m…
opened 02:45AM - 31 Jan 20 UTC
closed 07:05PM - 31 Jan 20 UTC
NeedsUserFeedback
Outdated
`Hugo Static Site Generator v0.62.1/extended darwin/amd64 BuildDate: unknown`
…
I needed a way to make markdown links open in a new tab with `target="_blank"`. Was able to do this in Blackfriday with the `hrefTargetBlank` config option, but for Goldmark I followed the instructions [exactly as outlined here](https://discourse.gohugo.io/t/how-to-open-link-in-new-tab-with-hugos-new-goldmark-markdown-renderer-in-v0-62-0/22540). This works, but for some reason it produces a single space character directly after the link. I've ensured `render-link.html` doesn't have any trailing spaces or lines.
Let me know if this issue would be better filed under the Goldmark library.
Source:
<img width="803" alt="Screen Shot 2020-01-30 at 19 41 39" src="https://user-images.githubusercontent.com/911274/73508254-9c06c200-4398-11ea-9a79-53e7efc7bcfc.png">
Rendered:
<img width="704" alt="Screen Shot 2020-01-30 at 19 42 32" src="https://user-images.githubusercontent.com/911274/73508281-afb22880-4398-11ea-912b-5a377cbe5b67.png">
Console:
<img width="912" alt="Screen Shot 2020-01-30 at 19 40 38" src="https://user-images.githubusercontent.com/911274/73508250-9ad59500-4398-11ea-866e-63ab403e4bba.png">
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. */ -}}
3 Likes
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.
system
Closed
August 13, 2020, 10:05pm
7
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.