🔗

[Hugo] How to add target='_blank' attribute to a link

Method

Add a setting to config.toml as below.

[blackfriday]
    hrefTargetBlank = true

Blackfriday is a library to convert .md to .html.

How about internal links?

Let’s refer document.

hrefTargetBlank

default: false

Blackfriday flag: HTMLHREFTARGET_BLANK

Purpose: true opens external links absolute links in a new window or tab. While the target=”_blank” attribute is typically used for external links, Blackfriday does that for all absolute links.

━━Blackfriday Options

According to docs, in default, an absolute link is opened in a new tab and a relative tag is opened in the same tag.

Actually, I got the following results.

[abs](http://localhost:1313/posts/) # Opend in new tab
[rel](/posts/)                      # Opend in same tab

But we need to use 2 links properly. It’s too much hassle.

So, I give you an idea.

One needs to make note of this if they use absolute links throughout, for internal links too (for example, by setting canonifyURLs to true or via absURL).

━━Blackfriday Options

Follow this, add a setting to config.toml again.

canonifyURLs = true

canonifyURLs is an option that relative URLs in articles are displayed as it is or converted to absolute URLs.

When this is false, URLs are displayed as it is. When this is true, those are converted.