Can I use a shortcode within Markdown link text?

I have a shortcode for rendering abbreviations. As it’s simplest, the shortcode is called with {{< abbr "html" >}}, the shortcode template looks up a definition within .Data using the passed argument as a key and then outputs something like <abbr title="Hypertext Markup Language">HTML</abbr>.

However, I can’t use this shortcode within a Markdown link, e.g. check out the [{{< abbr "html" >}} standard](https://html.spec.whatwg.org/multipage/); it gets rendered literally as check out the <a href="https://html.spec.whatwg.org/multipage/">{{< abbr "html" >}}</a>.

Is there anyway to force Hugo to process the shortcodes in the content file first, before passing the result to the Markdown renderer? I can’t see anything in the Goldmark configuration that would be relevant here.

Or, is it possible to use something like resources.ExecuteAsTemplate within the Markdown render hook for a link? For reference, my render-link.html file is:

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

First, I am unable to reproduce the problem as described.

git clone --single-branch -b hugo-forum-topic-44801 https://github.com/jmooring/hugo-testing hugo-forum-topic-44801
cd hugo-forum-topic-44801
hugo server

Second, your render hook really doesn’t do anything, other than open external destinations in a new tab, which most users find annoying.

2 Likes

Thanks for the usability link; since I can automatically add the rel=noreferrer with the Referrer-Policy header, I’ve done away with the render hook entirely and can now confirm that the shortcodes-within-Markdown-links are working as intended.

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