Making external links open in a new tab by using the replaceRE function to change the output of .Content

Hello, I would like links with an https in their address to open in a separate tab, meaning they should have the target="_blank" attribute applied to them. However, I only want this to apply to links within articles.

I searched the forum and found that this can be achieved by modifying the _default/_markup template that renders the links, but since I’m using Emacs Org Mode, that solution doesn’t apply to me. However, I came across another post that suggested an alternative method, which seems to work:

<article>
  <h2>Title</h2>
  {{ $content := .Content }}
  {{ $content = replaceRE `<a href="(https?://.+)">` `<a href="$1" target="_blank" class="ext-link">` $content | safeHTML }}
  {{ $content }}
</article>

The problem is that I’m using .Content in four different files, so I would need to add the same code to each one. That feels a bit redundant, but if that’s the only way to do it, I’ll go ahead with that.

Try link render hook.

Save the following code as layouts/_default/_markup/render-link.html.

{{- $u := urls.Parse .Destination -}}
<a href="{{ .Destination | safeURL }}"
  {{- with .Title }} title="{{ . }}"{{ end -}}
  {{- if $u.IsAbs }} rel="external" target="_blank"{{ end -}}
>
  {{- with .Text }}{{ . }}{{ end -}}
</a>
{{- /* chomp trailing newline */ -}}

What about using a custom shortcode

1 Like

CyrusYip Thank you. The layouts/_default/_markup/render-link.html metdod doesn’t work with Org format. As I mentioned in my first post, I’m not using Markdown.

irkode Thank you. I will try. I didn’t know that shortcodes can be used for inline HTML elements as well.

I created a shortcode named extlink taking inspiration from the figure shortcode:

{{- if .Get "url" -}}
  <a href="{{ .Get "url" }}" target="_blank">
    {{ with .Get "description" }}{{ . }}{{ end }}
  </a>
{{- end -}}

then I call it like so:

Lorem ipsum {{< extlink url="https://domain.tld" description="Link text" >}} dolor sit.

I don’t use a closing {{< / extlink >}}. I think it’s fine in this case, though I’m not entirely sure. I would appreciate it if someone could point it out if I did anything wrong.

Since I haven’t received a response, I assume using the shortcode is acceptable. I was just asking because I wasn’t sure if shortcodes can work for inline elements without needing a closing shortcode tag.

2 Likes

I would like to write the conditional in such a way that if the description is not specified, the URL is included.

This is how it is now

{{- if .Get "url" -}}
  <a href="{{ .Get "url" }}" target="_blank">
    {{ with .Get "description" }}{{ . }}{{ end }}
  </a>
{{- end -}}

I can do that, like so:

{{- if .Get "url" -}}
  <a href="{{ .Get "url" }}" target="_blank">
    {{ if .Get "description" }}{{ .Get "description" }}{{ else }}{{ .Get "url" }}{{ end }}
  </a>
{{- end -}}

or would it be better to keep using with like this? :

{{- if .Get "url" -}}
  <a href="{{ .Get "url" }}" target="_blank">
    {{ with .Get "description" }}{{ . }}{{ else }}{{ .Get "url" }}{{ end }}
  </a>
{{- end -}}

I would be grateful for any suggestion.

You can simplify a bit…

{{- with .Get "url" -}}
  <a href="{{ . }}" target="_blank">
    {{- or ($.Get "description") . -}}
  </a>
{{- end -}}
1 Like

Thank you. I appreciate it. It seems like I’m missing out on a lot of features by using the org format. I need to check out ox-hugo, it seems like a better method, with org files being exported to markdown. Honestly, I didn’t know about it until recently.

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