Shortcode randomly rendered by different output format templates

My blog project has two output formats for articles, html and text. I’ve therefore created two variants of a figure shortcode, figure.html and figure.text. My HTML site renders okay, using the html shortcode for pages and lists, but for my feeds, it sometimes renders figure in the feed using the figure.html and sometimes using figure.text. It’s pretty annoying when it uses figure.text.

This leads me to a few questions:

  • How does Hugo determine which shortcode template to use, html or text?
  • Is there a way I can force it to use one in one context, and another in another?
  • Failing that, do I just need to stick with html until there is more output format support for shortcodes?

Thank you!

Can you try to rename one of those? It’s just a guess, but, it should probably work.

If I renamed it, it would work, because the shortcode name would be different. But I made them this way because I’d like to have the shortcode emit HTML in HTML contexts and plain text in other contexts (though I know that shortcodes don’t really support that right now).

Still, I suppose that’s they way to deal with it until shorcodes support output formats.

Unless there’s some way to detect the current output format in the template at runtime? Or if isPlainText is true?

This is obviously a bug that needs to get fixed. I would at least expect this to be deterministic, but as you experience, that may not be the case.

A workaround, which I guess only work if you have a small amount of shortcodes, would be to create a shortcode variant also for the feed (xml).

But could you create an issue on GH about this?

Okay, made #7774.

It somehow hadn’t occurred to me to create a shortcode for the feed format. :man_facepalming: I guess because the plain text output format doesn’t use the figure.text variant, either. But I’ll give it a try and see what happens.

Long term would be nice to have some control over the format for a particular use of the content. The JSONFeed template, for example, emits both HTML and text. Would be nice if, in in entry.text, I could call .Content.Text or something to indicate which output format in a particular context.

Creating figure.json and figure.xml does appear to fix the determinism problem, thank you!

1 Like