Indexing shortcodes

I’m working on a blog site for a writer who writes about wine. The blog posts refer to wines, grapes, wineries, and wine-producing regions, and the goal is for each post to link to a corresponding page that indexes the relevant posts. For example, if post A refers to the Tannat grape, then the word Tannat would be a link to ‘/grape/tannat’, and the page at that location would contain a link back to post A.

In some ways, this is a classic use-case for taxonomies: if I have a taxonomy for ‘grapes’, then my post just needs to contain:

grapes=[‘tannat’, ‘cabernet-sauvignon’]

etc. and a lot of the behavior I want will fall out automatically.

There are two isses, however. One is that some of my taxonomies are hierarchical: for example, the wine producing region of Mendoza is in Argentina, so I want a URL something like ‘/region/argentina/mendoza’. As far as I can tell, Hugo doesn’t support hierarchical taxonomies: all taxonomies are essentially ‘flat’.

The other is a DRY issue. I can use a custom shortcode to generate the link, i.e. my text might look like:

… this wine contains the {{< grape tannat >}}Tannat{{< /grape >}} grape …

but I’m forced to repeat myself: to get everything to work right, I need to add ‘tannat’ to the taxonomy in the frontmatter, and include the shortcode to generate the link in the text. What I’d really like is for something in the build to detect that ‘grape tannat’ shortcode, and automatically add ‘tannat’ to the list of taxonomy items for ‘grape’.

Is there a good way to do this in Hugo? I’m not averse to writing a pre-processor that scans the posts and extracts the shortcode data to build other content files if needed, but it would be nice if that could be tied into the Hugo build system so that it would run automatically, instead of having to be invoked as a separate step.

So my questions are:

  • can Hugo support hierarchy in taxonomies?
  • is there a way to run a pre-processor of some kind automatically as part of the build?
  • can I avoid a pre-processor entirely, by somehow indexing the content of shortcodes?

Thank you in advance for any suggestions.

“Hierarchy” can mean a bunch, but I don’t think it does what you want.

If I were going about this, I’d probably use only front matter for tags, and replace all instances of the terms in content with links to the term page. There are many examples of folks replacing text, search these forums. :slight_smile:

If your “hierarchy” is 1:1, meaning only in Argentinia they produce Mendoza then it’s not a taxonomy but a folder structure:

content/region/argentinia/ << list of  sub posts
content/region/argentinia/mendoza/ << post for mendoza

you would work with a custom template that gets the sub posts listed. I think.

If a wine type is produced in multiple regions we have a problem. I am not a wine drinker :wink:

But from how I read your question you don’t want to load a page winetype/lambrusco and expect to see posts from italy, spain and so on in there, would you? If so then you would create a lambrusco page bundle inside of each region it applies to.

If you want to list per region and per winetype then, you could still use a taxonomy winetype within the regions post type.

And sorry for my wine related incompetence… I am a beer guy :beer: