internal/godoc/dochtml/internal/render: test table of contents

Also, tweak spacing to make it look more like handwritten HTML.

Change-Id: Iefd2ffad42ad83ebcb246e931f239f8605b04c23
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/412319
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
diff --git a/internal/godoc/dochtml/internal/render/linkify_test.go b/internal/godoc/dochtml/internal/render/linkify_test.go
index 0d6b8ad..87ded26 100644
--- a/internal/godoc/dochtml/internal/render/linkify_test.go
+++ b/internal/godoc/dochtml/internal/render/linkify_test.go
@@ -540,3 +540,37 @@
 		}
 	}
 }
+
+func TestTOC(t *testing.T) {
+	doc := `
+Documentation.
+
+The Go Project
+
+Go is an open source project.
+
+Heading 2
+
+More text.`
+
+	want := testconversions.MakeHTMLForTest(`<div role="navigation" aria-label="Table of Contents">
+    <ul class="Documentation-toc">
+      <li class="Documentation-tocItem">
+          <a href="#hdr-The_Go_Project">The Go Project</a>
+        </li>
+      <li class="Documentation-tocItem">
+          <a href="#hdr-Heading_2">Heading 2</a>
+        </li>
+      </ul>
+  </div>
+<p>Documentation.
+</p><h4 id="hdr-The_Go_Project">The Go Project <a class="Documentation-idLink" href="#hdr-The_Go_Project">¶</a></h4><p>Go is an open source project.
+</p><h4 id="hdr-Heading_2">Heading 2 <a class="Documentation-idLink" href="#hdr-Heading_2">¶</a></h4><p>More text.
+</p>`)
+
+	r := New(context.Background(), nil, pkgTime, &Options{EnableCommandTOC: true})
+	got := r.declHTML(doc, nil, false).Doc
+	if diff := cmp.Diff(want, got, cmp.AllowUnexported(safehtml.HTML{})); diff != "" {
+		t.Errorf("r.declHTML() mismatch (-want +got)\n%s", diff)
+	}
+}
diff --git a/internal/godoc/dochtml/internal/render/render.go b/internal/godoc/dochtml/internal/render/render.go
index 383a2ba..9ba2f53 100644
--- a/internal/godoc/dochtml/internal/render/render.go
+++ b/internal/godoc/dochtml/internal/render/render.go
@@ -61,15 +61,15 @@
 var docDataTmpl = template.Must(template.New("").Parse(`
 {{- if and .EnableCommandTOC .Headings -}}
   <div role="navigation" aria-label="Table of Contents">
-    <ul class="Documentation-toc {{if gt (len .Headings) 5}}Documentation-toc-columns{{end}}">
-      {{- range .Headings -}}
-		<li class="Documentation-tocItem">
-		  <a href="#{{.ID}}">{{.Title}}</a>
-		</li>
-      {{- end -}}
+    <ul class="Documentation-toc{{if gt (len .Headings) 5}} Documentation-toc-columns{{end}}">
+      {{range .Headings -}}
+        <li class="Documentation-tocItem">
+          <a href="#{{.ID}}">{{.Title}}</a>
+        </li>
+      {{end -}}
     </ul>
   </div>
-{{- end -}}
+{{end -}}
 {{- range .Elements -}}
   {{- if .IsHeading -}}
     <h4 id="{{.ID}}">{{.Title}} <a class="Documentation-idLink" href="#{{.ID}}">¶</a></h4>