I also need to to this for figure and table captions.
I need this because my requirements state that links to headings, figures, and tables must show the respective section, figure, or table number like this: “See 1.2.1. Heading 3”
Render hooks are not an option as they can only add ephemeral numbers that cannot be targeted. I can’t use CSS counters as well because the PDF generation toolkit I’m using does not understand custom counters.
Add the numbers manually. This is prone to error and labor intensive, particularly if you start moving thing around.
Use a regex replacement on content. Now you have two problems.
Use a render hook.
I’m not sure what that means. You can store a counter (heading render hooks don’t have an Ordinal) and current level in a Page store then access them the next time the hook is fired (the are fired sequentially top-to-bottom). I haven’t thought a lot about how to code this, but it could be complicated.
Keep in mind, if you use a heading render hook and modify the heading element’s id attribute, you will not be able to use the TableOfContents method because the IDs are determined before the hook. You could use something like tocbot.js instead.
To add a counter to table captions, create a table render hook and use its Ordinal.
To add a counter to a shortcode you must use a Page store. Unlike render hook ordinals, shortcode ordinals are shared across all shortcodes; they are not tracked separately for each shortcode.
Argh. That’s bad. Maybe looking into the “PDF generation toolkit” might help. I have only used MPDF, a PHP library. That one understands counters.
Without knowing the specifics, there are tools like Remark that might work directly on the markdown files and could be used to update the numbering automatically. I am still relatively sure this should/could be done outside of Hugo. In Hugo it will be renderhooks (for the headers and figures at least) with Store and in the case of tables regexpes. That would have to run every time the site is built which is counter productive. I am a fierce proponent of content hygiene outside of Hugo… That’s all ;)=
So far so good. This will take care of keeping the number order. However, I’ll also need to access the numbering from Link render hooks. What’s more, I’ll need to access the target page’s Store from other pages where the links to it are located. Is this possible?
That’s excellent news.
Can I override it to add Ordinal in a template? Where’s the code at?
Maybe not the best term, but that’s what I meant when I said ephemeral. I’m OK with that because I’m only messing with the .print.html render hook in Docsy, leaving the .html unchanged. I don’t need TableOfContents on the print page.
I’ll also need to access the numbering from Link render hooks. What’s more, I’ll need to access the target page’s Store from other pages where the links to it are located.
My response was scoped to your original request: how to add section numbering to headings without using regex replacements.
I suggest you build a prototype then experiment on your own.