htmlmarkdownhugohugo-shortcode

Can I create links with 'target="_blank"' in hugo posts content using Markdown?


I use Hugo to deploy static web, using Markdown and the problem I would like to sort out is simple. I would like to find the simplest and compatible with Markdown language way to make my web links open in different tab. The method I use now is below:

  1. < rawhtml >}}<a href="https://en.wikipedia.org/wiki/Rasmus_Lerdorf" target="blank">Rasmus Lerdorf</a></br>{{< /rawhtml >
  2. [link with title](http://nodeca.github.io/pica/demo/ "target="blank")

First method working but it is HTML second method not working. Thank You.


Solution

  • You need to create a new file at /layouts/_default/_markup/ called render-link.html.

    In that file, you can then customise it to something like:

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

    Here is what would happen:

    It will only add it to URLs with "http" and "https".

    The documentation for render-hooks is available here: https://gohugo.io/templates/render-hooks/.