toc

package
v0.8.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package toc generates and renders tables of contents. It builds a hierarchical structure from headings and renders HTML navigation.

Index

Constants

View Source
const PageSlotAttr = "data-toc-page"

PageSlotAttr marks the element that holds an entry's printed page number. The PDF generator prints the document once to learn which page each anchor landed on, then fills these slots in and prints again — the slot is left empty and reserved on the first pass so that filling it in cannot change the layout, and therefore cannot invalidate the very page numbers being written.

Variables

This section is empty.

Functions

func CountEntries

func CountEntries(entries []TOCEntry) int

CountEntries returns the total number of TOC entries, including descendants.

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator builds hierarchical TOC trees from flat heading lists.

func NewGenerator

func NewGenerator() *Generator

NewGenerator creates a new TOC generator that renders an English heading.

func NewGeneratorForLanguage added in v0.8.0

func NewGeneratorForLanguage(language string) *Generator

NewGeneratorForLanguage creates a TOC generator whose rendered heading is written in the book's language.

func (*Generator) Generate

func (g *Generator) Generate(headings []HeadingInfo) []TOCEntry

Generate builds a hierarchical TOC tree from document-order headings.

func (*Generator) RenderHTML

func (g *Generator) RenderHTML(entries []TOCEntry) string

RenderHTML renders the TOC tree as nested HTML navigation.

type HeadingInfo

type HeadingInfo struct {
	Level int    // Heading level from 1 to 6.
	Text  string // Heading text.
	ID    string // Unique heading identifier, typically used as an anchor.
	// Section is the SUMMARY.md part heading this chapter starts, carried on
	// the chapter's own top-level entry. The printed contents renders it as
	// an unlinked group label; it used to be dropped, flattening a grouped
	// book into one undifferentiated list.
	Section string
}

HeadingInfo stores heading metadata parsed from Markdown.

type TOCEntry

type TOCEntry struct {
	Level    int        // Heading level from 1 to 6.
	Title    string     // Heading text.
	ID       string     // Unique identifier used for anchor links.
	Section  string     // Part heading this entry opens; see HeadingInfo.Section.
	PageNum  int        // Optional page number for print-oriented output.
	Children []TOCEntry // Child entries.
}

TOCEntry represents one node in the TOC tree.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL