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 (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-06-19 12:19:46 -04:00
|
|
|
// Headings holds the top level headings.
|
|
|
|
type Headings []Heading
|
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 {
|
2019-11-06 14:10:47 -05:00
|
|
|
ID string
|
|
|
|
Text string
|
|
|
|
|
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 {
|
2019-11-06 14:10:47 -05:00
|
|
|
return h.ID == "" && h.Text == ""
|
|
|
|
}
|
|
|
|
|
2019-11-23 10:59:43 -05:00
|
|
|
// Root implements AddAt, which can be used to build the
|
|
|
|
// data structure for the ToC.
|
2019-11-06 14:10:47 -05:00
|
|
|
type Root struct {
|
2021-06-19 12:19:46 -04:00
|
|
|
Headings Headings
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
|
2021-06-19 12:19:46 -04:00
|
|
|
// AddAt adds the heading into the given location.
|
|
|
|
func (toc *Root) AddAt(h Heading, row, level int) {
|
|
|
|
for i := len(toc.Headings); i <= row; i++ {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-19 12:19:46 -04: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 {
|
|
|
|
heading.Headings = append(heading.Headings, Heading{})
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
2021-06-19 12:19:46 -04: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.
|
2019-12-12 01:48:40 -05:00
|
|
|
func (toc Root) ToHTML(startLevel, stopLevel int, ordered bool) string {
|
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()
|
|
|
|
return b.s.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-06-19 12:19:46 -04: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() {
|
|
|
|
b.s.WriteString("<a href=\"#" + h.ID + "\">" + h.Text + "</a>")
|
|
|
|
}
|
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).
|
|
|
|
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
|
|
|
}
|