feat: two-pass table of contents with linked, numbered entries - #9
Merged
Merged
Conversation
--toc prepends a table of contents whose page numbers are found by fixed point: render, read back the heading->page map the shim already collects for the PDF outline, rebuild the block, re-render — the TOC's own length shifts every page after it, so one pass can never know its numbers. Entries link to their sections (#mtoc-N anchors injected into the headings), --toc-depth caps the listing (1-6, default chapters only), --toc-title renames it, and --pageless drops the numbers. The settle loop in Markd::Pdf.settled_pages also absorbs the kdp gutter iteration (page count depends on margins and on the TOC block alike), and the CLI's kdp path now goes through the library loop, which learns to pass mirror_headers through. The forced break to the body rides an empty div because the shim's page-break properties inherit down the tree — a break on the nav would give every entry its own page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
markpdf --tocprepends a table of contents with real page numbers, where every entry links to its section. This closes the gap KDP.md documented — "The tool has no two-pass TOC; write it by hand after the layout settles."--toc— prepend the TOC--toc-depth N— deepest heading level listed, 1 (chapters only) to 6 (default 1)--toc-title T— heading above the list (default "Contents")--pageless+--toc— entries without page numbers (there are no pages)toc:,toc_depth:,toc_title:kwargs onMarkd::Pdf.render/render_to_memoryHow
The C++ shim already computed each heading's page for the PDF outline and threw the mapping away. It now also returns a heading map (
index\tlevel\tpage\ttitleper heading) to Crystal.On top of that,
Markd::Pdf.settled_pagesruns the numbers to a fixed point: render → read where the headings actually landed → rebuild the TOC block → re-render, until what the TOC says matches where things are. The TOC's own length shifts every page after it, so one pass can never know its numbers. Two or three passes converge; the cap is 6 with a stderr warning if a pathological document never settles.Entries are clickable:
rewrite_heading_anchorsinjects a unique#mtoc-Nanchor into every heading with visible text (same numbering the shim's map uses), and each entry links to it via the shim's existing internal-link machinery. The vendored markd'stoc?anchors are deliberately not used (lib/ is off-limits and not duplicate-safe).Notable findings along the way
html_tag.cpp:347resolves custom properties through ancestors). Apage-break-after: alwayson the nav gave every TOC entry its own page (1-page doc → 8 pages). The forced break to the body now rides an empty div after the nav.--mirror-headersbeing dropped in kdp mode.<div>, not an<h1>, so it never self-lists, triggers the kdp recto rule, or lands in the bookmarks.Verification
spec/pdf_toc_spec.cr: map parsing, anchor numbering alignment, block building/filters, heading-map layout, the real two-pass test (40 chapters: every TOC number equals the page where that chapter actually renders), kdp recto+filler numbering, pageless, CLI flag handlingamebaclean; all four binaries build (shards build); shim builds (make -C ext)--toc-depthabortsDocs: README (flag reference + a TOC section) and KDP.md (gap paragraph replaced with how the TOC plays with recto fillers and the even-page pad).