Not sure if this has been covered before, but I just added multiple code blocks on the same page and have added anchorLineNos = true. This doesn’t generate unique IDs, thus always the first available line number is the one the browser scrolls to.
The only workaround I know around this is to specify line starting numbers for each of the code blocks and make sure they’re different. It can’t always be used especially if the code blocks are of different languages. Instead of this, it would be better if IDs are generated something like #count-of-codeblock-1 where count-of-codeblock could be replaced with the position of codeblock on page or maybe a custom name that we can specify.
Use the shortcode instead of the three backticks should work or make it less complicated to adjust this. I made my own shortcode when I ran into the same (I think) issue, but can’t find it right now… probably for a customer. If I find it I’ll add a link here.
Take the code above and save in layouts/shortcodes/highlight.html and then add parameters. A random ID can be generated with the following snippet:
My own opinion, I am not a Hugo developer: No. I am not a fan of these internal templates and shortcodes. If you look for issues with for instance the Instagram shortcode and now newly the Twitter shortcode and look for issues with Google Analytics and it’s new formats and tracking codes it becomes quite clear, that this is an immense amount of developer hours that has to go into adding, maintaining these snippets. And that’s not what Hugo is for. I sometimes feel like Hugo as a car manufacturer, that gives you for some of your “drive to targets” a driver and others it doesn’t. I never use internal templates… But as I wrote, that is only my opinion.
These internal layouts stem from a time, where Hugo was used by a small group of techies and everyone basically used the same features. It is more mature now and IMHO it’s much easier to create a sample layout somewhere that people can copy into their projects for things like this.
Last point: I would assume that Goldmark at some point starts adding ids, classes and so on to markdown, the same way as it now allows that for headers only. That would be the point where the circle closes, because then you can go back to the three backticks and just add your own ID.
After some time of trying to understand the code, I think I might have realised what it’s doing. It’s just generating random values based on Site Title and current Unix time, I guess? Maybe I need to replace the original IDs with these randomly generated ones? If this part is correct, I still don’t know how to do it.
Hmm… I thought that the highlight shortcode has some form of loop where you could add it. Let me get back to you with a template. Right now you don’t “hand over” $random to the shortcode creation.