Nathan
Nathan

Reputation: 7709

Write a link that does not get converted to an <a/> tag in a markdownified string with hugo

I have a text in my hugo config file, which gets markdownified in the html template. Inside this text I want to have a link but for legal reasons I do not want to convert this link to a "clickable" link. But if I just write

Some text http://example.com some more text

markdownify converts this link to an <a/> tag making it clickable.

Can I prevent this but still use markdownify on the text? The link should still be copy-pastable.

Upvotes: 3

Views: 1606

Answers (2)

mshjri
mshjri

Reputation: 169

I found this gist with different ways to prevent auto-linking.

Method 1: Add HTML tags in the hyperlink text

Some text http://<span></span>example.com some more text

returns:

Some text http://example.com some more text

Method 2: Escape a character from the hyperlink text (works with Hugo 0.51 but not with Github)

Some text http\://example.com some more text

returns:

Some text http://example.com some more text

(I should note that those are still tricks/hacks since there is no official way to disable auto-linking in markdownify.)

Upvotes: 2

revelt
revelt

Reputation: 2410

Here's an excerpt from my config.toml (which sits in the root folder):

  testme = "This **link** is not linking to url at all"

I address this custom field anywhere, for example, in my head partial:

{{ replace (.Site.Params.testme | markdownify) "url" "https://codeandsend.com" | safeHTML }}

Here's what it does:

  1. {{ .Site.Params.testme | markdownify }} pipes testme value from config.toml into the markdownify function.

  2. I then replace all occurencies of string url with desired address. You could use any other placeholder instead of url, but beware of system-reserved names.

  3. | safeHTML pipes the result into HTML entities decoder. There's no such thing in Hugo as instruction to skip entity encoding — only to decode post factum, and safeHTML does this.

Result: bold text using markdown and no link encoding:

enter image description here

Upvotes: 1

Related Questions