Pass the ref shortcode as an argument to a custom shortcode

There’s a GH issue out there, in which the user would like to pass the ref shortcode as an argument to a custom shortcode.

Per the issue details, the shortcode is named blank_url.html, and defined as:

<a target="_blank" href="{{ .Get 1 }}">{{ .Get 0 | markdownify }}</a>

And the proposed usage is (which is not currently possible):

{{< blank_url "here" (ref "projects/limecv.md") >}}

One way to get the desired behavior is to use .Inner.

So if the shortcode was instead defined as:

<a target="_blank" {{ printf "href=%q" .Inner | safeHTMLAttr }}>{{ .Get 0 | markdownify }}</a>

Its usage would then be:

{{< blank_url "here" >}}{{< ref "projects/limecv.md" >}}{{< /blank_url >}}

Which would output:

<a target="_blank" href="http://localhost:1313/projects/limecv/">here</a>
2 Likes

I quickly read that issue and I suspect that that will never be implemented (by me, that is). That particular use case could easily (and more cleanly) be implemented by calling .Page.GetPage inside that shortcode.

1 Like

Nice, that does indeed make the usage cleaner. For others reading this:

Updated definition:

{{ $page := .Page.GetPage (.Get 1) }}
<a target="_blank" {{ printf "href=%q" $page.Permalink | safeHTMLAttr }}>{{ .Get 0 | markdownify }}</a>

Updated usage:

{{< blank_url "here" "projects/limecv.md" >}}
4 Likes

To make the shortcode work with both internal and external resources.

Updated definition:

{{ $text := .Get 0 }}
{{ $ref := .Get 1 }}

{{ if (in $ref "//") }}
  <a target="_blank" {{ printf "href=%q" $ref | safeHTMLAttr }}>{{ $text | markdownify }}</a>
{{ else }}
  {{ $page := .Page.GetPage $ref }}
  <a target="_blank" {{ printf "href=%q" $page.Permalink | safeHTMLAttr }}>{{ $text | markdownify }}</a>
{{ end }}

Updated usage:

{{< blank_url "internal" "projects/limecv.md" >}}

{{< blank_url "external" "https://gohugo.io/" >}}