hugo: Table of contents links are incorrect when layout includes element in

With Hugo version v0.40.3 using the {{ .TableOfContents }} variable when one has a <base href="$.Site.BaseURL"> in a <HEAD> section, and using hugo server fails with Firefox (both ESR and 5x.x

Specifically the relative references for page fragments (e.g. href="#nameofheading") get turned into http://localhost:1313/#nameofheading instead of properly being relative (.e.g. on a /section1/page1/ you should get http://localhost:1313/section1/page1/#nameofheading).

I’m not not sure if this is a browser issue or a Hugo issue, when using BaseURL (as required by the theme site, becase of issue https://github.com/gohugoio/hugo/issues/2575)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 5
  • Comments: 16 (3 by maintainers)

Most upvoted comments

Depending on your use case, I was able to make similar adjustments in the template using:

{{ replace .TableOfContents `href="#` (printf `href="%s#` .Permalink) | safeHTML }}

Hope that this helps.

@digitalcraftsman for ToC does it still just generated #anchors even with BaseURL, and is there a way to configure it to generate the right URL when using BaseURL?

This issue has been discussed before. I will redo the ToC integration in the not so distant future and will have this in mind.

Curious: Why do you need base href?