1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
// 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"
)
// Headers holds the top level (h1) headers.
type Headers []Header
// Header holds the data about a header and its children.
type Header struct {
ID string
Text string
Headers Headers
}
// IsZero is true when no ID or Text is set.
func (h Header) IsZero() bool {
return h.ID == "" && h.Text == ""
}
// Root implements AddAt, which can be used to build the
// data structure for the ToC.
type Root struct {
Headers Headers
}
// AddAt adds the header into the given location.
func (toc *Root) AddAt(h Header, row, level int) {
for i := len(toc.Headers); i <= row; i++ {
toc.Headers = append(toc.Headers, Header{})
}
if level == 0 {
toc.Headers[row] = h
return
}
header := &toc.Headers[row]
for i := 1; i < level; i++ {
if len(header.Headers) == 0 {
header.Headers = append(header.Headers, Header{})
}
header = &header.Headers[len(header.Headers)-1]
}
header.Headers = append(header.Headers, h)
}
// ToHTML renders the ToC as HTML.
func (toc Root) ToHTML(startLevel, stopLevel int, ordered bool) string {
b := &tocBuilder{
s: strings.Builder{},
h: toc.Headers,
startLevel: startLevel,
stopLevel: stopLevel,
ordered: ordered,
}
b.Build()
return b.s.String()
}
type tocBuilder struct {
s strings.Builder
h Headers
startLevel int
stopLevel int
ordered bool
}
func (b *tocBuilder) Build() {
b.writeNav(b.h)
}
func (b *tocBuilder) writeNav(h Headers) {
b.s.WriteString("<nav id=\"TableOfContents\">")
b.writeHeaders(1, 0, b.h)
b.s.WriteString("</nav>")
}
func (b *tocBuilder) writeHeaders(level, indent int, h Headers) {
if level < b.startLevel {
for _, h := range h {
b.writeHeaders(level+1, indent, h.Headers)
}
return
}
if b.stopLevel != -1 && level > b.stopLevel {
return
}
hasChildren := len(h) > 0
if hasChildren {
b.s.WriteString("\n")
b.indent(indent + 1)
if b.ordered {
b.s.WriteString("<ol>\n")
} else {
b.s.WriteString("<ul>\n")
}
}
for _, h := range h {
b.writeHeader(level+1, indent+2, h)
}
if hasChildren {
b.indent(indent + 1)
if b.ordered {
b.s.WriteString("</ol>")
} else {
b.s.WriteString("</ul>")
}
b.s.WriteString("\n")
b.indent(indent)
}
}
func (b *tocBuilder) writeHeader(level, indent int, h Header) {
b.indent(indent)
b.s.WriteString("<li>")
if !h.IsZero() {
b.s.WriteString("<a href=\"#" + h.ID + "\">" + h.Text + "</a>")
}
b.writeHeaders(level, indent, h.Headers)
b.s.WriteString("</li>\n")
}
func (b *tocBuilder) indent(n int) {
for i := 0; i < n; i++ {
b.s.WriteString(" ")
}
}
// DefaultConfig is the default ToC configuration.
var DefaultConfig = Config{
StartLevel: 2,
EndLevel: 3,
Ordered: false,
}
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
// Whether to produce a ordered list or not.
Ordered bool
}
|