badboy / mdbook-toc

A preprocessor for mdbook to add inline Table of Contents support.
Mozilla Public License 2.0
163 stars 20 forks source link

TOC not generated? #36

Closed janilahti closed 1 year ago

janilahti commented 1 year ago

My TOC doesn't seem to get generated for some reason. Running this in Github action, and everything seems to go fine with latest mdbook (v0.4.30), mdbook-toc (v0.12.0) and mdbook-pdf (v0.1.6):

[INFO] (mdbook::book): Book building has started
[INFO] (mdbook::book): Running the html backend
[INFO] (mdbook::book): Running the markdown backend
[INFO] (mdbook::book): Running the pdf backend
[INFO] (mdbook::renderer): Invoking the "pdf" renderer

But resulting HTML from toc.md looks like this:

<main>
<h1 id="table-of-contents"><a class="header" href="#table-of-contents">Table of Contents</a></h1>
<p>First marker</p>
<!-- toc -->
<p>Second marker</p>
</main>

NOTE: This was when I used alternative marker [[_TOC_]] (that gets stripped away) and left the default one just hanging there. If I go with default marker, the default string gets removed and [[_TOC_]] gets rendered as <p>[[<em>TOC</em>]]</p> (as it should). So it seems that marker is detected, but for some reasons nothing is filled in place.

SUMMARY.md contains pointer to file with toc marker along with some content I copied and pasted for reference:

[Table of Contents](toc.md)

In book.toml I have

[preprocessor.toc]
command = "mdbook-toc"
renderer = ["html"]
marker = "[[_TOC_]]"

Turning on debug I get:

$ grep -i toc debug
[DEBUG] (mdbook::book::book): Loading Table of Contents (toc.md)
[DEBUG] (mdbook::preprocess::cmd): Checking if the "toc" preprocessor supports "html"
[DEBUG] (mdbook::book): Running the toc preprocessor.
[DEBUG] (handlebars::render): Rendering helper: "toc", params: [], hash: {}
[DEBUG] (handlebars::render): Rendering helper: "toc", params: [], hash: {}
[DEBUG] (handlebars::render): Rendering helper: "toc", params: [], hash: {}
[DEBUG] (mdbook::renderer::html_handlebars::hbs_renderer): Creating toc.html
[DEBUG] (mdbook::utils::fs): Creating /home/runner/work/mdpdftst/mdpdftst/book/html/toc.html
[DEBUG] (handlebars::render): Rendering helper: "toc", params: [], hash: {}
[DEBUG] (handlebars::render): Rendering helper: "toc", params: [], hash: {}
[DEBUG] (handlebars::render): Rendering helper: "toc", params: [], hash: {}
[DEBUG] (handlebars::render): Rendering helper: "toc", params: [], hash: {}
[DEBUG] (mdbook::preprocess::cmd): Checking if the "toc" preprocessor supports "markdown"
[DEBUG] (mdbook::book): Running the toc preprocessor.
[DEBUG] (mdbook::utils::fs): Creating /home/runner/work/mdpdftst/mdpdftst/book/markdown/toc.md
[DEBUG] (mdbook::preprocess::cmd): Checking if the "toc" preprocessor supports "pdf"
[DEBUG] (mdbook::book): Running the toc preprocessor.

I won't paste it as it's just too much, but there's also trace available: https://github.com/janilahti/mdpdftst/actions/runs/5132890322/jobs/9234773868

What am I missing here?

badboy commented 1 year ago

The marker is replaced with exactly your table of contents: nothing.

mdbook-toc collects all headers from the file after the marker to generate the table of contents. There's no header in your toc.md and thus nothing is generated. Adding ## Hello World at the end of toc.md correctly generates this in the HTML:

<ul>
<li><a href="#hello-world">Hello world</a></li>
</ul>
janilahti commented 1 year ago

Thank you for clearing this out. So, I'm not able to get full TOC of the book, only per file.

But one more note: If it works only for headers after the marker, then this

It turns this: <!-- toc --> into a Table of Contents based on all top- and second-level headings of the chapter.

is not exactly correct description.

badboy commented 1 year ago

Thank you for clearing this out. So, I'm not able to get full TOC of the book, only per file.

But one more note: If it works only for headers after the marker, then this

It turns this: <!-- toc --> into a Table of Contents based on all top- and second-level headings of the chapter.

is not exactly correct description.

I noticed that too. It started out with only top- and second-level headings, but later expanded to default 4 levels, but configurable. I'll update the docs.