// 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("") } 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("