Render-Link Functionality Not Working

I wish this were a trivial question. It is not. The behavior changed in version 0.55, and again in 0.60.

Version Shortcode Markdown HTML
0.54 {{ .Inner }} {{< foo >}}**bold**{{< /foo >}} **bold**
0.54 {{ .Inner }} {{% foo %}}**bold**{{% /foo %}} <strong>bold</strong>
0.54 <p>{{ .Inner }}</p> {{< foo >}}**bold**{{< /foo >}} <p>**bold**</p>
0.54 <p>{{ .Inner }}</p> {{% foo %}}**bold**{{% /foo %}} <p><strong>bold</strong></p>
 
0.55 {{ .Inner }} {{< foo >}}**bold**{{< /foo >}} **bold**
0.55 {{ .Inner }} {{% foo %}}**bold**{{% /foo %}} <p><strong>bold</strong></p>
0.55 <p>{{ .Inner }}</p> {{< foo >}}**bold**{{< /foo >}} <p>**bold**</p>
0.55 <p>{{ .Inner }}</p> {{% foo %}}**bold**{{% /foo %}} <p>**bold**</p>
 
0.60 {{ .Inner }} {{< foo >}}**bold**{{< /foo >}} **bold**
0.60 {{ .Inner }} {{% foo %}}**bold**{{% /foo %}} <p><strong>bold</strong></p>
0.60 <p>{{ .Inner }}</p> {{< foo >}}**bold**{{< /foo >}} <p>**bold**</p>
0.60 <p>{{ .Inner }}</p> {{% foo %}}**bold**{{% /foo %}} <!-- raw HTML omitted -->

Look at the last line.

The <!-- raw HTML omitted --> comment is rendered, without warning during the build, due to the security model introduced in Hugo 0.60. In this example, to get the desired output…

Shortcode:

<p>{{ .Inner | markdownify }}</p>

And then call it with:

{{< foo >}}**bold**{{< /foo >}}

So now we’re in the habit of coding our shortcodes templates this way, and always calling them with the {{< foo >}} notation. But there’s another big difference between the two notation styles, introduced in Hugo 0.55. From the release notes:

Shortcodes using the {{% %}} as the outer-most delimiter will now be fully rendered when sent to the content renderer (e.g. Blackfriday for Markdown), meaning they can be part of the generated table of contents, footnotes, etc.

I think this change is what makes it possible to do what you originally asked about: using the output of a shortcode within a render hook.

Comments:

  1. Prior to deployment we always grep the public directory for <!-- raw HTML omitted --> and ZgotmplZ to find problems that Hugo doesn’t warn us about during build.

  2. With Hugo 0.62 and later .RenderString is preferable to markdownify:

    {{ .Inner | .Page.RenderString }}

  3. The version 0.60 examples assume default settings (i.e., you have not overridden default security settings in config.toml).

  4. If you find this confusing you are not alone. The existing documentation is insufficient, but I’m not sure anyone understands the functionality/cases well enough to rewrite it.

8 Likes