Shortcode doesn't parse HTML returned from partial

I have a partial that generates links to related pages in a website about a data model.
I have gotten it to return a link that looks right using printf:

{{- $whole_link := printf "<a href=\"/reference/classes/%s>%s</a>" $link $name -}}

Which displays in the browser as this:
<a href="/reference/classes/activity/>Activity</a>

whereas it should (of course) be the actual link to the Activity page.

I have the Goldmark renderer set up to deal with HTML, and I have tried using “safeHTML” everywhere I can think that it might go.

I technically have a working solution… because it works fine if I build it at the shortcode level one-link-at-a-time. At the moment the code that generates those links is reproduced in 3 places in the code, and I need it in four or five more cases, which makes it sort of beg for refactoring into a partial.

Current solution (this works):

{{- with $object -}}
    {{ $lower := . | lower }}
    {{- $link:= partial "retrieve_class_link" $lower -}}
    {{- $name:= partial "retrieve_class_name" $lower -}}
    <a href="/reference/classes/{{ $link }} "> {{ $name }}</a>
{{- end -}}

Desired solution (refactor):

{{- with $object -}}
	{{- partial "class_link" . -}}
{{- end -}}

partial class_link.html:

{{ $lower := . | lower }}
{{- $link := partial "retrieve_class_link" $lower -}}
{{- $name := partial "retrieve_class_name" $lower -}}
{{- $whole_link := printf "<a href=\"/reference/classes/%s>%s</a>" $link $name -}}
{{- return $whole_link -}}

Can anybody tell me the missing piece?

How do you invoke the shortcode?
Depending on the invocation, Hugo treats the result differently:
{{% my-shortcode %}} vs {{< my-shortcode >}}
See Shortcodes | Hugo and the sections that follow for details.

You are missing a quotation mark, and you need to pass the string through the safeHTML function.

{{ $whole_link := printf `<a href="/reference/classes/%s">%s</a>` $link $name | safeHTML }}

Also, in a partial that returns a value with the return statement, you don’t need to worry about removing whitespace with the {{- -}} notation.

1 Like

Thank you! I was staring at it for so long, and I just couldn’t see it.
Also, the backticks rather than single quotes is a fiddly little bit that I had missed previously.

(I had tried the “| safeHTML” but of course it didn’t work with the missing quotation mark.)