Shortcodes Inside Front Matter?

I’ve looked at a number of posts dealing with issues of shortcode references in various contexts, including inside front matter, but I’m having a hard time getting my head around concepts like .RenderString and other solutions. For more detail, the problem I have is described in a recent blog post of mine: Adding a Custom 404 Page in Hugo // The Grinnell College Digital Library Application Developer's Blog.

We have a number of old, broken links in an online publication that uses Hugo. Rather than “removing” the broken links I’ve created a shortcode named broken to direct readers to a page with options for finding the missing information. That shortcode ALWAYS works locally when I run hugo server, and it works in staging and production when it’s used in an article’s Markdown content, but it does NOT work in staging or production when referenced from inside the front matter of an article.

Specifically, I have a snippet of code in one template, {{ $bio | markdownify }}, that applies Markdown rules to $bio, which is a field from front matter. If I call the broken shortcode from within a field like .bio the text of the shortcode reference appears rather than the link that the shortcode was supposed to render.

My workaround is to simply embed the HTML that the shortcode would have produced directly into the front matter field, but I don’t look forward to maintaining different practices depending on where the broken link resides.

Sorry for the limited explanation here. I hope the details at Adding a Custom 404 Page in Hugo // The Grinnell College Digital Library Application Developer's Blog might be helpful, and I appreciate any suggestions that others share. Thank you, and take care.

Are you trying to do something like this:

title = 'Post 3'
date = 2023-02-21T09:57:55-08:00
draft = false
foo = "{{< my-shortcode >}}"

Yes, and thank you for the example! In my case, foo contains a mix of markdown and the shortcode reference.

Sorry. but you can’t do that. You can only use shortcodes within the content of page, not in front matter.

Ok, thanks for confirming. I thought that might be the case but wasn’t absolutely sure. So, I’ve written two REGEX forms, one that turns Markdown links [like this](http:/just-an-example) into my broken shortcode, and another that just yields the raw HTML that broken would have produced.


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