# Using shortcode inside render-hook

**URL:** <https://discourse.gohugo.io/t/using-shortcode-inside-render-hook/42038>\
**Category:** support\
**Created:** [December 22, 2022, 7:16pm UTC](https://discourse.gohugo.io/t/using-shortcode-inside-render-hook/42038 "2022-12-22T19:16:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anmith](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@anmith](https://discourse.gohugo.io/u/anmith)\
**Post date:** [December 22, 2022, 7:16pm UTC](https://discourse.gohugo.io/t/using-shortcode-inside-render-hook/42038/1 "2022-12-22T19:16:53Z")

</div>

While playing with render hooks and shortcodes, I was trying to distinguish between several image sources within a render hook for a markdown link.

The link looks like this

```auto
[{{< image src="myimage.jpg" >}}](http://myurl)

```

The image shortcode is quite simple and consists of just two lines:

```auto
{{ $src := $.Get "src" }}
<img src="{{ $src }}" />

```

Last but not least the render hook, taken from the documentation

```auto
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if strings.HasPrefix .Destination "http" }} target="_blank" rel="noopener"{{ end }}>{{ .Text | safeHTML }}</a>

```

Within the render hook (located in render-link.html) I was trying to check if there’s an image (= the image shortcode) embedded, checking “.Text” or “.PlainText” for containing the string “img” or “image”.

To my surprise, neither “.Text” nor “.PlainText” contained any of these strings.

While investigating further using

```auto
<p>Length: {{ len .PlainText }}</p>
<p>Substr 1: {{ substr .PlainText 0 13 }}</p>
<p>Substr 2: {{ substr .PlainText 13 12 }}</p>

```

within “render-link.html”, I got the result

Length: 25  
Substr 1: HAHAHUGOSHORT  
Substr 2: CODE-s0-HBHB

Which translates to:

```auto
HAHAHUGOSHORTCODE-s0-HBHB

```

If I don’t split the string and leave “.PlainText” intact, the image is displayed instead of what should be “.PlainText”.

Conclusion: While running a render-hook, shortcodes are translated to an internal format and it’s impossible to check either for the original shortcode tag or the rendered tag.

Question: Is there a way to get either the original shortcode-string or the rendered output from within render-link.html?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 22, 2022, 7:19pm UTC](https://discourse.gohugo.io/t/using-shortcode-inside-render-hook/42038/2 "2022-12-22T19:19:30Z")

</div>

Change this:

```auto
{{< image src="myimage.jpg" >}}

```

To this:

```auto
{{% image src="myimage.jpg" %}}

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 22, 2022, 7:24pm UTC](https://discourse.gohugo.io/t/using-shortcode-inside-render-hook/42038/3 "2022-12-22T19:24:04Z")

</div>

And you’ll need this in your site configuration:

```auto
[markup.goldmark.renderer]
unsafe = true

```

It’s not unsafe if _you_ are in control of the content.

---

<div class="post-metadata">

**Author:** ![anmith](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@anmith](https://discourse.gohugo.io/u/anmith)\
**Post date:** [December 22, 2022, 7:41pm UTC](https://discourse.gohugo.io/t/using-shortcode-inside-render-hook/42038/4 "2022-12-22T19:41:21Z")

</div>

@jmooring  
Thanks, this really does the trick.

Do you have some additional documentation on how you came across this solution?  
I tired [Configure Markup |Hugo](https://gohugo.io/getting-started/configuration-markup/#goldmark) but wasn’t able to figure it out by myself.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 22, 2022, 8:15pm UTC](https://discourse.gohugo.io/t/using-shortcode-inside-render-hook/42038/5 "2022-12-22T20:15:34Z")

</div>

When you call a shortcode using the `{{< foo >}}` notation, we do not render the shortcode before:

1. Sending the page content to the markdown renderer (Goldmark), or
2. Hooking into links, images, and headings

Instead, we send a placeholder, effectively something like this:

```auto
HAHAHUGOSHORTCODE-UNIQUE-ID

```

When we get this back from Goldmark, we replace the placeholder with the rendered shortcode.

But you can also force your shortcode to be rendered _before_ we send the page content to Goldmark (and before we hook into links, images, and headings) by using this notation:

```auto
{{% div %}}  

```

But, by default, Goldmark considers HTML in markdown unsafe, so it replaces the HTML with this HTML comment:

```plaintext
<!-- raw HTML omitted -->

```

To tell Goldmark that HTML in markdown is safe in your situation:

```auto
[markup.goldmark.renderer]
unsafe = true

```

---

<div class="post-metadata">

**Author:** ![anmith](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@anmith](https://discourse.gohugo.io/u/anmith)\
**Post date:** [December 22, 2022, 9:22pm UTC](https://discourse.gohugo.io/t/using-shortcode-inside-render-hook/42038/6 "2022-12-22T21:22:42Z")

</div>

Thanks a lot for the explanation!

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [December 24, 2022, 9:23pm UTC](https://discourse.gohugo.io/t/using-shortcode-inside-render-hook/42038/7 "2022-12-24T21:23:34Z")

</div>

Closed on December 24, 2022, 9:23pm UTC

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
