Help us shape the future of Go! Take the 2026 Go Developer Survey

tableofcontents

package
v0.153.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	StartLevel: 2,
	EndLevel:   3,
	Ordered:    false,
}

DefaultConfig is the default ToC configuration.

View Source
var Empty = &Fragments{
	Headings:    Headings{},
	HeadingsMap: map[string]*Heading{},
}

Empty is an empty ToC.

Functions

This section is empty.

Types

type Builder

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

Builder is used to build the ToC data structure.

func (*Builder) AddAt

func (b *Builder) AddAt(h *Heading, row, level int)

AddAt adds the heading to the ToC.

func (Builder) Build

func (b Builder) Build() *Fragments

Build returns the ToC.

func (*Builder) SetIdentifiers

func (b *Builder) SetIdentifiers(ids []string)

SetIdentifiers sets the identifiers in the ToC.

type Config

type Config struct {
	// Heading start level to include in the table of contents, starting
	// at h1 (inclusive).
	// <docsmeta>{ "identifiers": ["h1"] }</docsmeta>
	StartLevel int

	// Heading end level, inclusive, to include in the table of contents.
	// Default is 3, a value of -1 will include everything.
	EndLevel int

	// Whether to produce a ordered list or not.
	Ordered bool
}

type Fragments

type Fragments struct {
	// Headings holds the top level headings.
	Headings Headings

	// Identifiers holds all the identifiers in the ToC as a sorted slice.
	// Note that collections.SortedStringSlice has both a Contains and Count method
	// that can be used to identify missing and duplicate IDs.
	Identifiers collections.SortedStringSlice

	// HeadingsMap holds all the headings in the ToC as a map.
	// Note that with duplicate IDs, the last one will win.
	HeadingsMap map[string]*Heading
}

Fragments holds the table of contents for a page.

func (*Fragments) ToHTML

func (toc *Fragments) ToHTML(startLevel, stopLevel any, ordered bool) (template.HTML, error)

ToHTML renders the ToC as HTML.

type Heading

type Heading struct {
	ID    string
	Level int
	Title string

	Headings Headings
}

Heading holds the data about a heading and its children.

func (Heading) IsZero

func (h Heading) IsZero() bool

IsZero is true when no ID or Text is set.

type Headings

type Headings []*Heading

Headings holds the top level headings.

func (Headings) FilterBy

func (h Headings) FilterBy(fn func(*Heading) bool) Headings

FilterBy returns a new Headings slice with all headings that matches the given predicate. For internal use only.

Jump to

Keyboard shortcuts

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