replaceRE returns quoted text instead of raw HTML

I’m having an issue with shortcode that uses strings.ReplaceRE; the replacement output is quoted, and therefore not interpreted as html. I can’t find a way to drop the quotes.

The shortcode in conversation.html:

{{ replaceRE `\*\*([AB])\*\*\: (.*)` "<p><strong>Message from $1:</strong><br />$2</p>" .Inner }}

Sample input:

{{<conversation>}}
**B**: Hello.
**A**: A: Hi there! How's it going?
{{</conversation>}}

Actual output:
A text element wrapped in quotes
image

Expected output:
HTML code that’s not wrapped in quotes.

I’d love to hear your feedback on my usage of ReplaceRE.

I searched this forum and found a similar issue: .Get returns string with double quotes - support - HUGO (gohugo.io), however, whether or not the shortcode includes any HTML tag, or not at all, the result of replaceRE is still wrapped in quotes. Also, this issue happens regardless of whether the REPLACEMENT is wrapped in “quotes” or backticks.

First, you are looking at your browser’s dev tools… that’s where the quotation marks are coming from. To accurately see what Hugo renders, view source in your browser (typically Ctrl+U).

Second, you need to pass the output through the safeHTML function:

{{ replaceRE `\*\*([AB])\*\*\: (.*)` "<p><strong>Message from $1:</strong><br />$2</p>" .Inner | safeHTML }}

Third, in case you weren’t aware of this, you can render markdown within the opening and closing shortcode tags by using the {{% %}} notation:

markdown

{{% conversation %}}
  **B**: Hello.

  **A**: Hi there! How's it going?
{{% /conversation %}}

shortcode

{{ .Inner }}

But I suspect the above is not relevant to your example.

1 Like