zwbetz-gh / cupper-hugo-theme

An accessibility-friendly Hugo theme, ported from the original Cupper project.
https://cupper-hugo-theme.netlify.app/
MIT License
301 stars 190 forks source link

Only <h2>showing in toc #62

Closed duncan-wither closed 3 years ago

duncan-wither commented 3 years ago

Describe the bug The table of contents (toc) generated doesn't include anything other than level two headers, ignoring the config.yaml settings.

To Reproduce example on the example site

Expected behaviour I'd expect it to behave in accordance with the config.yaml settings for the TOC.

Screenshots If applicable, add screenshots to help explain your problem.

Additional context A (possibly naiive) fix would be to change the toc.html partial to:

{{ $headings := findRE "<h2.*?>(.|\n])+?</h2>" .Content }}
{{ if ge (len $headings) 2 }}
  <nav class="toc" aria-labelledby="toc-heading">
    <h2 id="toc-heading">{{ T "table_of_contents" }}</h2>
    {{ .Page.TableOfContents }}
  </nav>
{{ end }}

However it left an extra <nav>. It's fine for me, I'm just not sure if that'll confuse the generator or someone at some point.

<nav class="toc" aria-labelledby="toc-heading">
    <h2 id="toc-heading">Table of contents</h2>
    <nav id="TableOfContents">

I think the proper fix would to change the toc.html to this:

<div>
    <h2>Table Of Contents</h2>
    {{ .Page.TableOfContents }}
</div>

And then adjust the css to use the generated class, but there'd be no aria-labelledby class, and I'm unsure what that's used for.

zwbetz-gh commented 3 years ago

Thanks for bringing this up.

Years back when first porting this theme, to only show h2 headings, you had to do some find-n-replace work. Now that Hugo supports a configurable Table of Contents, it makes since to use it.

Addressed in https://github.com/zwbetz-gh/cupper-hugo-theme/commit/87e1a8608f44d46f6cc34fc54a4ddadbd2999433

duncan-wither commented 3 years ago

Cheers for the fix