How to open plain URL links in a new tab?

TLDR: I’ve listed possible workarounds at the end of this post.

Assuming that you have not set linkify = false under [markup.goldmark.extensions] in your site’s config.toml file, the following markdown:

www.example.com  
<https://www.example.com>  
[https://www.example.com](https://www.example.com)  
[https://www.example.com][1]

[1]: https://www.example.com

produces this HTML:

<a href="http://www.example.com">www.example.com</a>
<a href="https://www.example.com">https://www.example.com</a>
<a href="https://www.example.com">https://www.example.com</a>
<a href="https://www.example.com">https://www.example.com</a>

Similarly, with email addresses, this markdown:

fake@example.com  
<fake@example.com>  
[fake@example.com](mailto:fake@example.com)  
[fake@example.com][2]

[2]: mailto:fake@example.com

produces this HTML:

<a href="mailto:fake@example.com">fake@example.com</a>
<a href="mailto:fake@example.com">fake@example.com</a>
<a href="mailto:fake@example.com">fake@example.com</a>
<a href="mailto:fake@example.com">fake@example.com</a>

In the markdown for each example, the first two link formats contain a destination only. The last two contain both text and destination. The markdown render hook is not fired for the link formats without text.

Recommendation

Create an issue on GitHub. I’m not sure if this is a bug or an enhancement.

Workarounds

1) Manually edit your markdown to use the [xxx](xxx) format. This is the safest of the workarounds that I have described.

2) Use a tool such as sed to perform a search/replace en masse, making sure that you backup all of your content files before proceeding. For example:

sed -E -i 's/<(https?:\/\/.*)>/[\1](\1)/' test.md
sed -E -i 's/<(.*@.*)>/[\1](mailto:\1)/' test.md

The above commands will convert this:

<https://www.example.com>
<http://www.example.com>
<fake@example.com>

to this:

[https://www.example.com](https://www.example.com)
[http://www.example.com](http://www.example.com)
[fake@example.com](mailto:fake@example.com)

3) In your single.html file(s), replace {{ .Content }} with:

{{ $content := .Content }}
{{ $content = replaceRE `<a href="(https?://.+)">` `<a href="$1" target="_blank">` $content | safeHTML }}
{{ $content = replaceRE `<a href="(mailto:.+)">` `<a href="$1" target="_blank">` $content | safeHTML }}
{{ $content | safeHTML }}

This will add a duplicate target="_blank" attribute to links that already have a target="_blank" attribute (golang doesn’t support negative lookaheads), but that won’t break anything. I have no idea what performance impact this will have when building a large site. It works, but I don’t like it very much.

1 Like