Code Monkey home page Code Monkey logo

Comments (9)

alex-shpak avatar alex-shpak commented on July 22, 2024 5

Hi!
In hugo version 0.60 they changed renderer for hugo, so now BookToC is a flag true|false
And levels are controlled by hugo

markup:
  tableOfContents:
    endLevel: 3
    ordered: false
    startLevel: 2

https://gohugo.io/getting-started/configuration-markup/#table-of-contents

from hugo-book.

kchen0x avatar kchen0x commented on July 22, 2024

OK, I found some documentation on Hugo official website: https://gohugo.io/content-management/toc/ which says:

Currently, the {{.TableOfContents}} page variable does not allow you to specify which heading levels you want the TOC to render. See the related GitHub discussion (#1778). As such, the resulting <nav id="TableOfContents"><ul></ul></nav> is going to start at <h1> when pulling from {{.Content}}.

So any suggestions or work-around?

from hugo-book.

kchen0x avatar kchen0x commented on July 22, 2024

OK I found the work-around.

{{/* https://github.com/gohugoio/hugo/issues/1778 */}}

{{/* ignore empty links with + */}}
{{- $headers := findRE "<h[2-3].*?>(.|\n])+?</h[2-3]>" .Content -}}
{{ .Scratch.Set "last_level" 1 }}

{{/* at least one header to link to */}}
{{- $has_headers := ge (len $headers) 1 -}}
{{- if $has_headers -}}

{{ $showToC := default (default true .Site.Params.BookShowToC) .Params.bookshowtoc }}
  {{ if and ($showToC) (.Page.TableOfContents) }}
  <aside class="book-toc fixed">
    <!-- {{ .Page.TableOfContents }} -->
    <nav id="TableOfContents">
      <summary>
      <b>Table of Contents</b>
      </summary>
      {{- range $headers -}}
          {{- $last_level := $.Scratch.Get "last_level" -}}
          {{- $header := . -}}
          {{- $base := ($.Page.File.LogicalName) -}}
          {{- $anchorId := ($header | plainify | htmlUnescape | anchorize) -}}
          {{- $href := delimit (slice $base $anchorId) "#" | string -}}
          {{- range findRE "[2-4]" . 1 -}}
              {{- $next_level := (int .) -}}
              {{- if gt $next_level $last_level -}}
                  {{- range seq (add $last_level 1) $next_level}}
                      <ul class="toc-h{{ . }}">
                  {{- end -}}
              {{- else if lt $next_level $last_level -}}
                  {{- range seq (add $next_level 1) $last_level}}
                      </ul>
                  {{- end -}}
              {{- end -}}
              <li><a href="{{ relref $.Page $href }}">{{- $header | plainify | htmlUnescape -}}</a></li>
              {{ $.Scratch.Set "last_level" $next_level }}
          {{- end -}}
      {{- end -}}
      </nav>
  </aside>
{{ end }}
{{- end -}}

This can limit side toc level to h2 and h3, you can adjust it when you want to set different depth of your toc.

from hugo-book.

kchen0x avatar kchen0x commented on July 22, 2024

Next problem need to solve is to config how deep the level you want to show the toc in specific post.

from hugo-book.

alex-shpak avatar alex-shpak commented on July 22, 2024

Hi!
Yeah, since ToC is provided by markdown library it's hard to tweak,
Have you considered to hide lower level toc with css? in assets/_custom.scss something like

nav#TableOfContents ul ul ul ul {
  display: none;
}

This will leave only top 3 levels visible. Not best solution, but something simpler.

from hugo-book.

kchen0x avatar kchen0x commented on July 22, 2024

oh, yes.

This is a simpler solution. But it's harder to make it configurable for individual post which has different level to show because of the hard coded css.

from hugo-book.

alex-shpak avatar alex-shpak commented on July 22, 2024

I think it should be fairly easy to make per page.

  1. Override inject/head.html
  2. If page variable bookToCLevels: x is set: generate <style>...</style> with CSS to hide levels

Not sure that I want to add it to theme, I try to keep KISS approach.

from hugo-book.

alex-shpak avatar alex-shpak commented on July 22, 2024

HI again!
I added feature which allows to limit amount of levels showed for ToC with CSS

Set this In site config or page frontmatter to render only h1, h2 and h3

BookToC: 3

from hugo-book.

ikingye avatar ikingye commented on July 22, 2024

I set BookToC: 4 in page, but not work

from hugo-book.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.