I'm seeing inconsistent output with Markdown and the ref shortcode

Hi,

With Markdown if you want the name and URL to be the same you can do <https://example.com>. This is working no problem with Hugo.

I’m seeing inconsistent behavior when using the ref shortcode:

  • <{{% ref "hello-world" %}}> returns the expected result which is a link
  • <{{< ref "hello-world" >}}> returns an empty string which produces no link tag
  • [{{< ref "hello-world" >}}({{< ref "hello-world" >}}) returns the expected result which is a link

I would have expected the 2nd and 3rd entries to produce the same result.

Does anyone know why the 2nd one doesn’t render a link?

Maybe this one: Shortcodes | Hugo

There’s a long story behind this, but the behavior is correct.

When you call a shortcode using the {{< >}} notation, we replace the shortcode call with a unique placeholder (a short string without whitespace) before sending the content to the markdown renderer (Goldmark). When we get the rendered content back from Goldmark, we replace the placeholder with the rendered shortcode.

With this markdown:

<{{< ref "hello-world" >}}>

We’re sending something like this to the markdown renderer:

<HAHAHUGOSHORTCODE-s0-HBH>

The markdown renderer interprets that as raw HTML, which for security reasons is omitted by default. You can check this yourself by viewing source in your browser. You’ll see this:

<!-- raw HTML omitted -->

If you want to allow raw HTML within your markdown, change your site configuration:

[markup.goldmark.renderer]
unsafe = true

If I were you I would use the the {{% %}} notation instead.

Also note that the related relref shortcode was made obsolete by render hooks.

1 Like

Thanks for the explanation. Although it’s interesting, I already have unsafe = true in my config and it still produces an empty string.

git clone --single-branch -b hugo-forum-topic-50784 https://github.com/jmooring/hugo-testing hugo-forum-topic-50784
cd hugo-forum-topic-50784
hugo server

Right, I mean with unsafe = true it still doesn’t render when using <{{< ref "hello-world" >}}>. The previous comment you wrote made it seem like setting unsafe would allow it to render?

It does render, but as an HTML tag, which you won’t see in the browser. View source in your browser to test.