I’m wondering if headings in a page can be accessed and iterated over from within a shortcode.
Imagine a content page like below:
One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin.
## My Heading
He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment.
### My Subheading
A collection of textile samples lay spread out on the table - Samsa was a travelling salesman
Is it possible to access a dictionary of headings and sub-headings of this page?
I’m guessing .Page.Contents would be a good place to start and maybe a where filter to extract headings and construct a dictionary?
I also see that there’s a .Page.TableOfContents variable that seems to be exactly what I need. But it seems to be the rendered html version, What I need is a nested dictionary of headings and sub headings within a page.
.Contents is already HTML. So you’d have to search for <h(\d)>([^<]+)</h\1> with a regular expression. Then the text of the headings would be accessible in the second capturing group, their level in the first one. However, afaik, a dictionary is a key-value map, so I don’t see how that will help you to retain the hierarchical structure of your headlines.
Answering your broader question of what I’m trying to do. I’m trying to override the way headingID is generated by Hugo, by using a markup hook for headings.
I need heading IDs to be in English even if the heading text is not English. For example for a given page in French, I can construct a list of this page headings (French headings) using the code above. I’m hoping to fetch the English version of the same page and do the same, then correlate the order of headings and apply English IDs to the French headings.
The part that I’m stuck now is getting the English page, seems like that scope is not accessible in the heading hooks.
This will not find a heading on the first line of your file because of the leading \n. Also, the ‘#’ marker(s) are followed by a space in markup, which your RE gobbles up in the capturing group. Is that intended?