-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
tableofcontents.go
251 lines (212 loc) · 5.48 KB
/
tableofcontents.go
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// 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 (
"html/template"
"sort"
"strings"
"github.com/gohugoio/hugo/common/collections"
)
// 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
}
// AddAt adds the heading to the ToC.
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
}
// Headings holds the top level headings.
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
}
// Heading holds the data about a heading and its children.
type Heading struct {
ID string
Level int
Title string
Headings Headings
}
// IsZero is true when no ID or Text is set.
func (h Heading) IsZero() bool {
return h.ID == "" && h.Title == ""
}
func (h *Heading) walk(fn func(*Heading)) {
fn(h)
for _, h := range h.Headings {
h.walk(fn)
}
}
// Fragments holds the table of contents for a page.
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
}
// addAt adds the heading into the given location.
func (toc *Fragments) addAt(h *Heading, row, level int) {
for i := len(toc.Headings); i <= row; i++ {
toc.Headings = append(toc.Headings, &Heading{})
}
if level == 0 {
toc.Headings[row] = h
return
}
heading := toc.Headings[row]
for i := 1; i < level; i++ {
if len(heading.Headings) == 0 {
heading.Headings = append(heading.Headings, &Heading{})
}
heading = heading.Headings[len(heading.Headings)-1]
}
heading.Headings = append(heading.Headings, h)
}
// ToHTML renders the ToC as HTML.
func (toc *Fragments) ToHTML(startLevel, stopLevel int, ordered bool) template.HTML {
if toc == nil {
return ""
}
b := &tocBuilder{
s: strings.Builder{},
h: toc.Headings,
startLevel: startLevel,
stopLevel: stopLevel,
ordered: ordered,
}
b.Build()
return template.HTML(b.s.String())
}
func (toc Fragments) walk(fn func(*Heading)) {
for _, h := range toc.Headings {
h.walk(fn)
}
}
type tocBuilder struct {
s strings.Builder
h Headings
startLevel int
stopLevel int
ordered bool
}
func (b *tocBuilder) Build() {
b.writeNav(b.h)
}
func (b *tocBuilder) writeNav(h Headings) {
b.s.WriteString("<nav id=\"TableOfContents\">")
b.writeHeadings(1, 0, b.h)
b.s.WriteString("</nav>")
}
func (b *tocBuilder) writeHeadings(level, indent int, h Headings) {
if level < b.startLevel {
for _, h := range h {
b.writeHeadings(level+1, indent, h.Headings)
}
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.writeHeading(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) writeHeading(level, indent int, h *Heading) {
b.indent(indent)
b.s.WriteString("<li>")
if !h.IsZero() {
b.s.WriteString("<a href=\"#" + h.ID + "\">" + h.Title + "</a>")
}
b.writeHeadings(level, indent, h.Headings)
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).
// <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
}