# Escape shortcodes when processing Markdown using RenderString

**URL:** https://discourse.gohugo.io/t/escape-shortcodes-when-processing-markdown-using-renderstring/55200
**Category:** support
**Created:** [July 2, 2025, 12:14am UTC](https://discourse.gohugo.io/t/escape-shortcodes-when-processing-markdown-using-renderstring/55200 "2025-07-02T00:14:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![dboulet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dboulet/32/10095_2.png) [@dboulet](https://discourse.gohugo.io/u/dboulet)
#### Post date: [July 2, 2025, 12:14am UTC](https://discourse.gohugo.io/t/escape-shortcodes-when-processing-markdown-using-renderstring/55200/1 "2025-07-02T00:14:47Z")

</div>

I’m having trouble escaping shortcodes when passing them through the [RenderString](https://gohugo.io/methods/page/renderstring/) function. I have some Markdown which contain shortcodes inside of code blocks and I want to prevent Hugo from processing the shortcodes.

Here’s a reduced test case: let’s say I put this into a shortcode template:

````auto
{{ print 
    "```go-html-template\n" 
    `{{< qr text="https://gohugo.io" />}}` 
    "\n```" 
    | .Page.RenderString 
}}

````

Output:

 ![Screenshot 2025-07-01 at 7.04.21 PM](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/3X/9/1/9176f2a98402b65227258d65a4f9b9efe4113b0f.png)

This will output a code block with a QR code in it. This is not what I want.

The [conventional solution](https://discourse.gohugo.io/t/shortcode-syntax-example-within-a-code-block-is-being-executed/35494) is to escape shortcodes using something like `{{</* foo */>}}` but that’s not working in this case.

````auto
{{ print 
    "```go-html-template\n" 
    `{{</* qr text="https://gohugo.io" /*/>}}` 
    "\n```" 
    | .Page.RenderString 
}}

````

Output:  
 ![Screenshot 2025-07-01 at 7.04.54 PM](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/optimized/3X/4/0/40254b8a8cbbb99a5afa1b0b811e6aef9c550fdc_2_517x27.png)

This outputs a code block which contains `{{</* qr text="https://gohugo.io" /*/>}}`. The escape characters are left intact.

How do I escape shortcodes inside Markdown when using RenderString?

---

<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: [July 2, 2025, 5:10am UTC](https://discourse.gohugo.io/t/escape-shortcodes-when-processing-markdown-using-renderstring/55200/2 "2025-07-02T05:10:57Z")

</div>

See [https://github.com/gohugoio/hugo/issues/10058](https://github.com/gohugoio/hugo/issues/10058).

Can you call your shortcode using the `{{% %}}` notation instead?

````plaintext
{{% sc %}}
Some **other** text.

```go-html-template {style=friendly}
{{</* qr text="https://gohugo.io" /*/>}}
```

Some **other** text.
{{% /sc %}}

````

layouts/\_shortcodes/sc.html

```plaintext
{{ .Inner }}

```

rendered:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/3X/2/f/2faa5ae00befd749cc88f6f54f6134033062761a.png)

---

<div class="post-metadata">

### Author: ![dboulet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dboulet/32/10095_2.png) [@dboulet](https://discourse.gohugo.io/u/dboulet)
#### Post date: [July 2, 2025, 3:15pm UTC](https://discourse.gohugo.io/t/escape-shortcodes-when-processing-markdown-using-renderstring/55200/3 "2025-07-02T15:15:31Z")

</div>

Thanks for the suggestion, @jmooring.

In my case, the Markdown content is being imported from an external source and my shortcode doesn’t use `.Inner`.

I would have expected the `RenderString` function and the content renderer to treat escaped shortcodes in the exact same manner but that doesn’t seem to be the case.

---

<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: [July 2, 2025, 6:21pm UTC](https://discourse.gohugo.io/t/escape-shortcodes-when-processing-markdown-using-renderstring/55200/4 "2025-07-02T18:21:34Z")

</div>

You might consider substitution.

```plaintext
{{ $s := `
Some **other** text.

~~~go-html-template
{{< qr text="https://gohugo.io" />}}
~~~

Some **other** text.
`
}}

{{ $s | strings.ReplaceRE "{{<" "@@@" | .Page.RenderString | strings.ReplaceRE "@@@" "{{<" | safeHTML }}

```

where “@@@” is any string that is unlikely to appear anywhere. You only need to replace one of the delimiters.

The only reason I’m using `~~~` is to make this example work where the input is multiline. Your input can use triple backticks.

And the only reason I’m using `strings.ReplaceRE` instead of `strings.Replace` is that I can pipe—the argument order of `strings.Replace` isn’t great.

---

<div class="post-metadata">

### Author: ![dboulet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dboulet/32/10095_2.png) [@dboulet](https://discourse.gohugo.io/u/dboulet)
#### Post date: [July 3, 2025, 4:09am UTC](https://discourse.gohugo.io/t/escape-shortcodes-when-processing-markdown-using-renderstring/55200/5 "2025-07-03T04:09:02Z")

</div>

Thanks again, @jmooring, I appreciate the help.

One work-around I found was to replace the triple backticks with the `{{< highlight >}}` shortcode. Escaping seems to work reliably when the code is inside that shortcode.

Something like this works:

```auto
{{ print 
    "{{< highlight go-html-template >}}\n" 
    `{{</* qr text="https://gohugo.io" /*/>}}` 
    "\n{{< /highlight >}}" 
    | .Page.RenderString 
}}

```

---

<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: [July 3, 2025, 4:12am UTC](https://discourse.gohugo.io/t/escape-shortcodes-when-processing-markdown-using-renderstring/55200/6 "2025-07-03T04:12:23Z")

</div>

Perhaps I misunderstood. If what you’re receiving is always a code example, why not use the `transform.Highlight` template function directly?

---

<div class="post-metadata">

### Author: ![dboulet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dboulet/32/10095_2.png) [@dboulet](https://discourse.gohugo.io/u/dboulet)
#### Post date: [July 3, 2025, 4:26am UTC](https://discourse.gohugo.io/t/escape-shortcodes-when-processing-markdown-using-renderstring/55200/7 "2025-07-03T04:26:05Z")

</div>

The examples that I gave above were simplified for clarity. The actual strings being rendered in my custom shortcode contain more than just code. They often also include things like paragraphs and headings written in Markdown, interspersed with code samples.
