'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 1:[1]

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:

  • [link1](../something/ title="title") => <a href="../something/" title="title">link1</a>
  • [link2](https://example.com) => <a href="https://example.com">link2</a>

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/.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DharmanBot