Extract data from a shortcode to display it anywhere

I (with the help of the Hugo community :black_heart:) found a way to store data with a first shortcode and display this data anywhere (after this first post and this second). It’s something like an index function (index in the sense of a book index): I can identify a fragment of text in a first page, and call it in another page (or in the same page) and display context information (which page, etc.).

I use it to identify a definition in a text, and display this definition in another place. For me it’s very useful to write a concept definition directly in a Markdown file with a first shortcode (definition.html), and being able to call it back with a second shortcode (call.html) in another Markdown file (or in the same).

A demo repo is available here.

I was stuck on the store process because sometimes I called the data before it was stored (@jmooring kindly reminded me that is a simple logical issue). I just forced this process with that range at the beginning of the based layout (in the head):

{{ range site.RegularPages }}
{{ $noop := .WordCount }}
{{ end }}

It’s a bit brutal, and this adds build time (it can make a significant difference), but in my case it’s not a big deal.

(Spoiler: I use it for my PhD thesis, which will be available online (website and PDF) in ~3 weeks.)

1 Like