BlackFriday AutoHeadingIDs extension not working

I’m attempting to add the ability for titles within my generated pages to be linked to. At first I thought I needed to do this myself so I went to the HugoDocs repo to learn how you did it.

I found anchorforid.js that did exactly what I was looking for, but it depends on having each header having an id. So I went hunting to see how you did that.

This was a harder find as I couldn’t find anchorized used in the code base, which I had assumed was required to make this work. After some time I realized that this was a feature coming from BlackFriday so I started looking there and found AutoHeadingIDs.

From what I can tell AutoHeadingIDs is supposed to be enabled by default. In my case, however, my headers are rendered without an id. I also tried imitating the config.toml settings that HugoDocs used but it didn’t seem to work for me.

[blackfriday]
plainIDAnchors = true

Is there some magic to enable this? Perhaps it only adds ids if headers are contained within a particular element of some sorts?

Thank you in advance!

It would be easier to help you if we could see your code. If I were debugging this, I’d markup a doc with headings, and then view .TableOfContents to see what is happening. You can use the example from https://gohugo.io/content-management/toc/#usage.

You’re right, @maiki, having the code readily available would have helped a lot. I should have created a test project to demonstrate the issue. After reading your comment I did exactly that.

With a brand new project using the Hugo Primer theme I found the header ids were generated automatically for me perfectly!

While trying to understand why I realized that some of my headers in my original project were actually having ids generated for them as expected while others were not. I’m a little embarrassed to explain why but in the hopes of helping others who experience the same issue I’ll share. :slightly_smiling_face:

It turns out the “broken” headers that were missing their ids were coming from a custom shortcode that I added. Since the contents of the shortcode were not going through BlackFriday there was no chance whatsoever for the AutoHeadingIDs feature to help me. It makes perfect sense to me now but at the time I was completely lost. Thank you for helping me to get out of that maze. :heart:

For anyone else who experiences something similar, here’s what I did in my shortcode to make things work:

<h2 id="{{anchorize $title}}">{{$title}}</h2>
1 Like