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

1.7k views Asked by At

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.

2

There are 2 answers

4
I'M YourOnly.One On BEST ANSWER

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

0
NothingToSeeHere On

Or you could just put the link in as you have it using regular, old HTML.

IMHO, beating yourself up over the failings of Hugo and whatever theme is in use is counter-productive. Markdown was supposed to be a quick and dirty way for users who couldn't grasp the basic fundamentals of html to create and display content on a wiki.

Think about the current situation. Does using Hugo, a theme, and Markdown really save any effort when they simply can't produce the output you need because the features you need don't exist in that combination natively?

When you need to spend hours/days researching and learning how to manipulate the generator's template to generate the output you need from Markdown and Hugo, tell me, where exactly are those time savings?

This is why people using tools such as Flare, RoboHTML, and Paligo laugh at users who brag about how fast they Hugo site generates html pages.