Skip to content

Commit 33c0c0e

Browse files
committed
Fixed indent depth
1 parent a9295d7 commit 33c0c0e

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

ghdoc.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,25 +150,40 @@ func (doc *GHDoc) GrabToc() *GHToc {
150150
maxDepth = int(MaxHxDepth)
151151
}
152152

153+
hdrs := findHeadersInString(doc.html)
154+
155+
minHxDepth := MaxHxDepth
156+
for _, hdr := range hdrs {
157+
if hdr.Depth < minHxDepth {
158+
minHxDepth = hdr.Depth
159+
}
160+
}
161+
153162
toc := GHToc{}
154-
for _, hdr := range findHeadersInString(doc.html) {
163+
for _, hdr := range hdrs {
155164
// DEBUG BEGIN
156165
log.Printf("*** CHUCK: GrabToc hdr: %+#v", hdr)
157166
log.Printf("*** CHUCK: GrabToc minDepth: %+#v", minDepth)
158167
log.Printf("*** CHUCK: GrabToc maxDepth: %+#v", maxDepth)
159168
// DEBUG END
160169
hDepth := int(hdr.Depth)
161170
if hDepth >= minDepth && hDepth <= maxDepth {
162-
toc = append(toc, doc.tocEntry(listIndentation(), hdr))
171+
indentDepth := int(hdr.Depth) - int(minHxDepth) - doc.StartDepth
172+
// DEBUG BEGIN
173+
log.Printf("*** CHUCK: GrabToc minHxDepth: %+#v", minHxDepth)
174+
log.Printf("*** CHUCK: GrabToc doc.StartDepth: %+#v", doc.StartDepth)
175+
log.Printf("*** CHUCK: GrabToc indentDepth: %+#v", indentDepth)
176+
// DEBUG END
177+
indent := strings.Repeat(listIndentation(), indentDepth)
178+
toc = append(toc, doc.tocEntry(indent, hdr))
163179
}
164180
}
165181

166182
return &toc
167183
}
168184

169185
func (doc *GHDoc) tocEntry(indent string, hdr Header) string {
170-
indentDepth := int(hdr.Depth) - doc.StartDepth
171-
return strings.Repeat(indent, indentDepth) + "* " +
186+
return indent + "* " +
172187
"[" + doc.tocName(hdr.Name) + "]" +
173188
"(" + doc.tocLink(hdr.Href) + ")"
174189
}

ghdoc_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ For example:</p>
9595
Depth: 0,
9696
Indent: 2,
9797
}
98+
// DEBUG BEGIN
99+
log.Printf("*** CHUCK: ========")
100+
// DEBUG END
98101
toc := *doc.GrabToc()
99102
for i := 0; i <= len(tocExpected)-1; i++ {
100103
if toc[i] != tocExpected[i] {
@@ -234,16 +237,7 @@ func TestGrabTocStartDepth(t *testing.T) {
234237
StartDepth: 1,
235238
Indent: 2,
236239
}
237-
// DEBUG BEGIN
238-
log.Printf("*** CHUCK: =======")
239-
// DEBUG END
240240
toc := *doc.GrabToc()
241-
// DEBUG BEGIN
242-
log.Printf("*** CHUCK toc: ")
243-
for idx, item := range toc {
244-
log.Printf("*** CHUCK %d: %+#v", idx, item)
245-
}
246-
// DEBUG END
247241
for i := 0; i <= len(tocExpected)-1; i++ {
248242
if toc[i] != tocExpected[i] {
249243
t.Error("Res :", toc[i], "\nExpected :", tocExpected[i])

0 commit comments

Comments
 (0)