Refer to frontmatter in markdown content and shortcode parameters?

Hi, this is kinda dumb, but I’m trying to avoid using a template here if possible.
This is the opposite of several people that wanted to use shortcodes in frontmatter.

I have some content that uses a particular url multiple times, as in

[route](link)

{{< figure attrlink="https://example.com" ... >}}

[link]: https://example.com

It would be super cool if I could have simply defined “link” in my frontmatter
so it wouldn’t have to be explicitly written multiple times in my content.

like:

---
mylink: https://example.com
---

[route]( $link )

{{< figure attrlink=$link ... >}}

Is there anyway to expand frontmatter in markdown itself (including parameters to shortcakes) without going to partials or templates?

See the built-in param shortcode:
https://gohugo.io/content-management/shortcodes/#param

markdown

+++
title = 'Post 1'
date = 2021-01-01T00:00:00-00:00
draft = false
mylink = 'https://gohugo.io'
+++

[route]({{< param "mylink" >}})

{{< figure src="something" >}}

Then modify your figure shortcode to access front matter under some condition…

{{ or .Params.attrlink .Page.Params.mylink }}

…so it looks for an attrlink parameter in the shortcode code call, falling back to the link defined in front matter. Or something.

1 Like

Thank you! Perfect!

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