2019-11-06 14:10:47 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package tableofcontents
|
|
|
|
|
|
|
|
import (
|
2023-10-08 19:59:36 -04:00
|
|
|
"html/template"
|
2023-02-11 10:20:24 -05:00
|
|
|
"sort"
|
2019-11-06 14:10:47 -05:00
|
|
|
"strings"
|
2023-02-11 10:20:24 -05:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/common/collections"
|
2019-11-06 14:10:47 -05:00
|
|
|
)
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
// Empty is an empty ToC.
|
|
|
|
var Empty = &Fragments{
|
|
|
|
Headings: Headings{},
|
|
|
|
HeadingsMap: map[string]*Heading{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builder is used to build the ToC data structure.
|
|
|
|
type Builder struct {
|
|
|
|
toc *Fragments
|
|
|
|
}
|
|
|
|
|
2023-05-18 05:05:56 -04:00
|
|
|
// AddAt adds the heading to the ToC.
|
2023-02-11 10:20:24 -05:00
|
|
|
func (b *Builder) AddAt(h *Heading, row, level int) {
|
|
|
|
if b.toc == nil {
|
|
|
|
b.toc = &Fragments{}
|
|
|
|
}
|
|
|
|
b.toc.addAt(h, row, level)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build returns the ToC.
|
|
|
|
func (b Builder) Build() *Fragments {
|
|
|
|
if b.toc == nil {
|
|
|
|
return Empty
|
|
|
|
}
|
|
|
|
b.toc.HeadingsMap = make(map[string]*Heading)
|
|
|
|
b.toc.walk(func(h *Heading) {
|
|
|
|
if h.ID != "" {
|
|
|
|
b.toc.HeadingsMap[h.ID] = h
|
|
|
|
b.toc.Identifiers = append(b.toc.Identifiers, h.ID)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
sort.Strings(b.toc.Identifiers)
|
|
|
|
return b.toc
|
|
|
|
}
|
|
|
|
|
2021-06-19 12:19:46 -04:00
|
|
|
// Headings holds the top level headings.
|
2023-02-11 10:20:24 -05:00
|
|
|
type Headings []*Heading
|
|
|
|
|
|
|
|
// FilterBy returns a new Headings slice with all headings that matches the given predicate.
|
|
|
|
// For internal use only.
|
|
|
|
func (h Headings) FilterBy(fn func(*Heading) bool) Headings {
|
|
|
|
var out Headings
|
|
|
|
|
|
|
|
for _, h := range h {
|
|
|
|
h.walk(func(h *Heading) {
|
|
|
|
if fn(h) {
|
|
|
|
out = append(out, h)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
2019-11-06 14:10:47 -05:00
|
|
|
|
2021-06-19 12:19:46 -04:00
|
|
|
// Heading holds the data about a heading and its children.
|
|
|
|
type Heading struct {
|
2023-02-11 10:20:24 -05:00
|
|
|
ID string
|
2023-12-02 15:55:54 -05:00
|
|
|
Level int
|
2023-02-11 10:20:24 -05:00
|
|
|
Title string
|
2019-11-06 14:10:47 -05:00
|
|
|
|
2021-06-19 12:19:46 -04:00
|
|
|
Headings Headings
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
|
2019-11-23 10:59:43 -05:00
|
|
|
// IsZero is true when no ID or Text is set.
|
2021-06-19 12:19:46 -04:00
|
|
|
func (h Heading) IsZero() bool {
|
2023-02-11 10:20:24 -05:00
|
|
|
return h.ID == "" && h.Title == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heading) walk(fn func(*Heading)) {
|
|
|
|
fn(h)
|
|
|
|
for _, h := range h.Headings {
|
|
|
|
h.walk(fn)
|
|
|
|
}
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
// Fragments holds the table of contents for a page.
|
|
|
|
type Fragments struct {
|
|
|
|
// Headings holds the top level headings.
|
2021-06-19 12:19:46 -04:00
|
|
|
Headings Headings
|
2023-02-11 10:20:24 -05:00
|
|
|
|
|
|
|
// 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
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
// addAt adds the heading into the given location.
|
|
|
|
func (toc *Fragments) addAt(h *Heading, row, level int) {
|
2021-06-19 12:19:46 -04:00
|
|
|
for i := len(toc.Headings); i <= row; i++ {
|
2023-02-11 10:20:24 -05:00
|
|
|
toc.Headings = append(toc.Headings, &Heading{})
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
|
2020-09-09 17:41:53 -04:00
|
|
|
if level == 0 {
|
2021-06-19 12:19:46 -04:00
|
|
|
toc.Headings[row] = h
|
2019-11-06 14:10:47 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
heading := toc.Headings[row]
|
2019-11-06 14:10:47 -05:00
|
|
|
|
2020-09-09 17:41:53 -04:00
|
|
|
for i := 1; i < level; i++ {
|
2021-06-19 12:19:46 -04:00
|
|
|
if len(heading.Headings) == 0 {
|
2023-02-11 10:20:24 -05:00
|
|
|
heading.Headings = append(heading.Headings, &Heading{})
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
2023-02-11 10:20:24 -05:00
|
|
|
heading = heading.Headings[len(heading.Headings)-1]
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
2021-06-19 12:19:46 -04:00
|
|
|
heading.Headings = append(heading.Headings, h)
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
|
2019-11-23 10:59:43 -05:00
|
|
|
// ToHTML renders the ToC as HTML.
|
2023-10-08 19:59:36 -04:00
|
|
|
func (toc *Fragments) ToHTML(startLevel, stopLevel int, ordered bool) template.HTML {
|
2023-02-11 10:20:24 -05:00
|
|
|
if toc == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2019-11-06 14:10:47 -05:00
|
|
|
b := &tocBuilder{
|
|
|
|
s: strings.Builder{},
|
2021-06-19 12:19:46 -04:00
|
|
|
h: toc.Headings,
|
2019-11-06 14:10:47 -05:00
|
|
|
startLevel: startLevel,
|
|
|
|
stopLevel: stopLevel,
|
2019-12-12 01:48:40 -05:00
|
|
|
ordered: ordered,
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
b.Build()
|
2023-10-08 19:59:36 -04:00
|
|
|
return template.HTML(b.s.String())
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (toc Fragments) walk(fn func(*Heading)) {
|
|
|
|
for _, h := range toc.Headings {
|
|
|
|
h.walk(fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 14:10:47 -05:00
|
|
|
type tocBuilder struct {
|
|
|
|
s strings.Builder
|
2021-06-19 12:19:46 -04:00
|
|
|
h Headings
|
2019-11-06 14:10:47 -05:00
|
|
|
|
|
|
|
startLevel int
|
|
|
|
stopLevel int
|
2019-12-12 01:48:40 -05:00
|
|
|
ordered bool
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *tocBuilder) Build() {
|
2019-11-23 10:59:43 -05:00
|
|
|
b.writeNav(b.h)
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
|
2021-06-19 12:19:46 -04:00
|
|
|
func (b *tocBuilder) writeNav(h Headings) {
|
2019-11-06 14:10:47 -05:00
|
|
|
b.s.WriteString("<nav id=\"TableOfContents\">")
|
2021-06-19 12:19:46 -04:00
|
|
|
b.writeHeadings(1, 0, b.h)
|
2019-11-06 14:10:47 -05:00
|
|
|
b.s.WriteString("</nav>")
|
|
|
|
}
|
|
|
|
|
2021-06-19 12:19:46 -04:00
|
|
|
func (b *tocBuilder) writeHeadings(level, indent int, h Headings) {
|
2019-11-06 14:10:47 -05:00
|
|
|
if level < b.startLevel {
|
|
|
|
for _, h := range h {
|
2021-06-19 12:19:46 -04:00
|
|
|
b.writeHeadings(level+1, indent, h.Headings)
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.stopLevel != -1 && level > b.stopLevel {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hasChildren := len(h) > 0
|
|
|
|
|
|
|
|
if hasChildren {
|
|
|
|
b.s.WriteString("\n")
|
|
|
|
b.indent(indent + 1)
|
2019-12-12 01:48:40 -05:00
|
|
|
if b.ordered {
|
|
|
|
b.s.WriteString("<ol>\n")
|
|
|
|
} else {
|
|
|
|
b.s.WriteString("<ul>\n")
|
|
|
|
}
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, h := range h {
|
2021-06-19 12:19:46 -04:00
|
|
|
b.writeHeading(level+1, indent+2, h)
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if hasChildren {
|
|
|
|
b.indent(indent + 1)
|
2019-12-12 01:48:40 -05:00
|
|
|
if b.ordered {
|
|
|
|
b.s.WriteString("</ol>")
|
|
|
|
} else {
|
|
|
|
b.s.WriteString("</ul>")
|
|
|
|
}
|
2019-11-06 14:10:47 -05:00
|
|
|
b.s.WriteString("\n")
|
|
|
|
b.indent(indent)
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 07:23:25 -05:00
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (b *tocBuilder) writeHeading(level, indent int, h *Heading) {
|
2019-11-06 14:10:47 -05:00
|
|
|
b.indent(indent)
|
|
|
|
b.s.WriteString("<li>")
|
|
|
|
if !h.IsZero() {
|
2023-02-11 10:20:24 -05:00
|
|
|
b.s.WriteString("<a href=\"#" + h.ID + "\">" + h.Title + "</a>")
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
2021-06-19 12:19:46 -04:00
|
|
|
b.writeHeadings(level, indent, h.Headings)
|
2019-11-06 14:10:47 -05:00
|
|
|
b.s.WriteString("</li>\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *tocBuilder) indent(n int) {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
b.s.WriteString(" ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 10:59:43 -05:00
|
|
|
// DefaultConfig is the default ToC configuration.
|
2019-11-06 14:10:47 -05:00
|
|
|
var DefaultConfig = Config{
|
|
|
|
StartLevel: 2,
|
|
|
|
EndLevel: 3,
|
2019-12-12 01:48:40 -05:00
|
|
|
Ordered: false,
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
// Heading start level to include in the table of contents, starting
|
|
|
|
// at h1 (inclusive).
|
2023-01-04 12:24:36 -05:00
|
|
|
// <docsmeta>{ "identifiers": ["h1"] }</docsmeta>
|
2019-11-06 14:10:47 -05:00
|
|
|
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
|
2019-12-12 01:48:40 -05:00
|
|
|
|
|
|
|
// Whether to produce a ordered list or not.
|
|
|
|
Ordered bool
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|