pietroppeter / nimibook

A port of mdbook to nim(ib)
https://pietroppeter.github.io/nimibook/
MIT License
76 stars 7 forks source link

Indentation of entries in sidebar in generated HTML is incorrect after sub-sections #73

Closed PhilippMDoerner closed 1 year ago

PhilippMDoerner commented 1 year ago

Given this nbook:

import nimibook

var book = initBookWithToc:
  entry("Should Be 1.", "book/bla.nim")
  section("Should be 2", "book/internals.nim"):    
    section("Should be 2.1", "internals/adders.nim"):
      entry("Should be 2.1.1", "adders/one_adder.nim")

  entry("Should be 3.", "CONTRIBUTING.md")
nimibookCli(book)

Running these commands:

nimble install -y nimib@#head nimibook@#head
nim c -d:release --mm:refc nbook.nim
./nbook init
./nbook build

Generates this Sidebar: image

The numbering is correct, but the indentation isn't. The indentation implies that "Should be 3" is a sub-entry of the section "Should be 3" instead of a sibling entry.

I assume the logic that generates HTML only goes back one indentation level instead of determining the correct indentation level.

pietroppeter commented 1 year ago

Thanks a lot for reporting and for the reproducible example. Definitely a bug, will look into it and let you know!

pietroppeter commented 1 year ago

Btw this is the code that needs to be looked at: https://github.com/pietroppeter/nimibook/blob/main/src/nimibook/renders.nim

pietroppeter commented 1 year ago

Note to self when fixing:

pietroppeter commented 1 year ago

reproducing the bug using doc render, will keep debugging later:

import nimibook / [tocs, renders]

let myToc = initToc:
  entry("Should Be 1.", "book/bla.nim")
  section("Should be 2", "book/internals.nim"):    
    section("Should be 2.1", "internals/adders.nim"):
      entry("Should be 2.1.1", "adders/one_adder.nim")

  entry("Should be 3.", "CONTRIBUTING.md")

echo myToc.render
#[
<ol class="chapter">
  <li class="chapter-item expanded ">
    <a href="{{path_to_root}}book/bla.html" tabindex="0"><strong aria-hidden="true">1.</strong> Should Be 1.</a>
  </li>
  <li class="chapter-item expanded ">
    <a href="{{path_to_root}}book/internals.html" tabindex="0"><strong aria-hidden="true">2.</strong> Should be 2</a>
  </li>
  <li>
    <ol class="section">
      <li class="chapter-item expanded ">
        <a href="{{path_to_root}}book/internals/adders.html" tabindex="0"><strong aria-hidden="true">2.1.</strong> Should be 2.1</a>
      </li>
      <li>
        <ol class="section">
          <li class="chapter-item expanded ">
            <a href="{{path_to_root}}book/internals/adders/one_adder.html" tabindex="0"><strong aria-hidden="true">2.1.1.</strong> Should be 2.1.1</a>
          </li>
        </ol>
      </li>
      <li class="chapter-item expanded ">
        <a href="{{path_to_root}}CONTRIBUTING.html" tabindex="0"><strong aria-hidden="true">3.</strong> Should be 3.</a>
      </li>
    </ol>
these two are missing from the output:
  </li>
</ol>
]# 
PhilippMDoerner commented 1 year ago

Yeah looks like it doesn't check at the end of a section block if it's about to end multiple section blocks at once and instead interprets the ending of two blocks ( the one for "Should be 2" and "Should be 2.1") as one block ending.

I played around with the setup a bit and if I write it like this for example it works (I didn't write this exact command, I wrote one for the owlkettle docs as that was faster to play around with, but this should do as well):

import nimibook

var book = initBookWithToc:
  entry("Should Be 1.", "book/bla.nim")
  section("Should be 2", "book/internals.nim"):    
    section("Should be 2.1", "internals/adders.nim"):
      entry("Should be 2.1.1", "adders/one_adder.nim") # Block 1 ends with this

    entry("Should be 2.2 and fix things", "book/bla2.nim") # Block 2 ends with this

  entry("Should be 3.", "CONTRIBUTING.md")
nimibookCli(book)
pietroppeter commented 1 year ago

indeed inside this else section I think we should iterate over level differences to close the correct number of sections: https://github.com/pietroppeter/nimibook/blob/main/src/nimibook/renders.nim#LL57 will test a fix