Skip to content

Commit 7f05d21

Browse files
findleyrgopherbot
authored andcommitted
internal/cmd/weave: print two levels of headings in TOC
Print up to two levels of headings, independent of the root depth of the table of contents. Change-Id: I75c07a5983b2af2639ebb9bf76e4189a7b114143 Reviewed-on: https://go-review.googlesource.com/c/example/+/703975 Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 53021d5 commit 7f05d21

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

internal/cmd/weave/weave.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// exceptions:
1010
//
1111
// If a line begins with "%toc", it is replaced with a table of contents
12-
// consisting of links to the top two levels of headers ("#" and "##").
12+
// consisting of links to the top two levels of headers below the %toc symbol.
1313
//
1414
// If a line begins with "%include FILENAME TAG", it is replaced with the lines
1515
// of the file between lines containing "!+TAG" and "!-TAG". TAG can be omitted,
@@ -110,7 +110,11 @@ func main() {
110110
if line == "%toc" {
111111
toc = nil
112112
minTocDepth = 0
113-
} else if strings.HasPrefix(line, "# ") || strings.HasPrefix(line, "## ") {
113+
} else if strings.HasPrefix(line, "# ") ||
114+
strings.HasPrefix(line, "## ") ||
115+
strings.HasPrefix(line, "### ") ||
116+
strings.HasPrefix(line, "#### ") {
117+
114118
words := strings.Fields(line)
115119
depth := len(words[0])
116120
if minTocDepth == 0 || depth < minTocDepth {
@@ -140,7 +144,10 @@ func main() {
140144
switch {
141145
case strings.HasPrefix(line, "%toc"): // ToC
142146
for _, h := range toc {
143-
printf("%s1. [%s](#%s)\n", strings.Repeat("\t", h.depth-minTocDepth), h.text, h.anchor)
147+
// Only print two levels of headings.
148+
if h.depth-minTocDepth <= 1 {
149+
printf("%s1. [%s](#%s)\n", strings.Repeat("\t", h.depth-minTocDepth), h.text, h.anchor)
150+
}
144151
}
145152
case strings.HasPrefix(line, "%include"):
146153
words := strings.Fields(line)

0 commit comments

Comments
 (0)