Skip to content

Commit a9a10d0

Browse files
committed
content,internal/godoc: add table of contents to command pages
This change supports the table of contents section in the overview section of command pages. This helps give us feature parity with command pages on golang.org. Fixes golang/go#38073 Change-Id: I396fcebbc569d046fc6ca7bf8beed037c9321a1b Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/288553 Trust: Jamal Carvalho <[email protected]> Run-TryBot: Jamal Carvalho <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
1 parent ad9abe5 commit a9a10d0

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

content/static/css/stylesheet.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,19 @@ pre {
12741274
color: var(--gray-3);
12751275
margin-top: -0.5rem;
12761276
}
1277+
@media only screen and (min-width: 64rem) {
1278+
.Documentation-toc {
1279+
columns: 2;
1280+
white-space: nowrap;
1281+
}
1282+
}
1283+
.Documentation-toc:empty {
1284+
display: none;
1285+
}
1286+
.Documentation-tocItem {
1287+
text-overflow: ellipsis;
1288+
overflow: hidden;
1289+
}
12771290

12781291
.Documentation-tocItem--constants,
12791292
.Documentation-tocItem--funcsAndTypes,

internal/godoc/dochtml/dochtml.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import (
2525
"github.com/google/safehtml/legacyconversions"
2626
"github.com/google/safehtml/template"
2727
"github.com/google/safehtml/uncheckedconversions"
28+
"golang.org/x/pkgsite/internal"
2829
"golang.org/x/pkgsite/internal/derrors"
30+
"golang.org/x/pkgsite/internal/experiment"
2931
"golang.org/x/pkgsite/internal/godoc/dochtml/internal/render"
3032
"golang.org/x/pkgsite/internal/godoc/internal/doc"
3133
)
@@ -193,6 +195,7 @@ func renderInfo(ctx context.Context, fset *token.FileSet, p *doc.Package, opt Re
193195
return "/" + versionedPath
194196
},
195197
DisableHotlinking: true,
198+
EnableCommandTOC: experiment.IsActive(ctx, internal.ExperimentCommandTOC),
196199
})
197200

198201
fileLink := func(name string) safehtml.HTML {

internal/godoc/dochtml/internal/render/linkify.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ var (
6363
type docData struct {
6464
Elements []docElement
6565
DisablePermalinks bool
66+
EnableCommandTOC bool
6667
}
6768

6869
type docElement struct {
@@ -112,7 +113,8 @@ func (r *Renderer) declHTML(doc string, decl ast.Decl, extractLinks bool) (out s
112113
}
113114
}
114115
}
115-
out.Doc = ExecuteToHTML(r.docTmpl, docData{Elements: els, DisablePermalinks: r.disablePermalinks})
116+
out.Doc = ExecuteToHTML(r.docTmpl, docData{Elements: els,
117+
DisablePermalinks: r.disablePermalinks, EnableCommandTOC: r.enableCommandTOC})
116118
}
117119
if decl != nil {
118120
out.Decl = r.formatDeclHTML(decl, idr)

internal/godoc/dochtml/internal/render/render.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Renderer struct {
3636
packageURL func(string) string
3737
disableHotlinking bool
3838
disablePermalinks bool
39+
enableCommandTOC bool
3940
ctx context.Context
4041
docTmpl *template.Template
4142
exampleTmpl *template.Template
@@ -65,10 +66,29 @@ type Options struct {
6566
//
6667
// Only relevant for HTML formatting.
6768
DisablePermalinks bool
69+
70+
// EnableCommandTOC turns on the table of contents for the overview section
71+
// of command pages.
72+
//
73+
// Only relevant for HTML formatting.
74+
EnableCommandTOC bool
6875
}
6976

7077
// docDataTmpl renders documentation. It expects a docData.
7178
var docDataTmpl = template.Must(template.New("").Parse(`
79+
{{- if and .EnableCommandTOC .Elements -}}
80+
<div role="navigation" aria-label="Table of Contents">
81+
<ul class="Documentation-toc">
82+
{{- range .Elements -}}
83+
{{- if .IsHeading -}}
84+
<li class="Documentation-tocItem">
85+
<a href="#{{.ID}}">{{.Title}}</a>
86+
</li>
87+
{{- end -}}
88+
{{- end -}}
89+
</ul>
90+
</div>
91+
{{- end -}}
7292
{{- range .Elements -}}
7393
{{- if .IsHeading -}}
7494
<h4 id="{{.ID}}">{{.Title}}
@@ -99,6 +119,7 @@ func New(ctx context.Context, fset *token.FileSet, pkg *doc.Package, opts *Optio
99119
var packageURL func(string) string
100120
var disableHotlinking bool
101121
var disablePermalinks bool
122+
var enableCommandTOC bool
102123
if opts != nil {
103124
if len(opts.RelatedPackages) > 0 {
104125
others = opts.RelatedPackages
@@ -108,6 +129,7 @@ func New(ctx context.Context, fset *token.FileSet, pkg *doc.Package, opts *Optio
108129
}
109130
disableHotlinking = opts.DisableHotlinking
110131
disablePermalinks = opts.DisablePermalinks
132+
enableCommandTOC = opts.EnableCommandTOC
111133
}
112134
pids := newPackageIDs(pkg, others...)
113135

@@ -117,6 +139,7 @@ func New(ctx context.Context, fset *token.FileSet, pkg *doc.Package, opts *Optio
117139
packageURL: packageURL,
118140
disableHotlinking: disableHotlinking,
119141
disablePermalinks: disablePermalinks,
142+
enableCommandTOC: enableCommandTOC,
120143
docTmpl: docDataTmpl,
121144
exampleTmpl: exampleTmpl,
122145
ctx: ctx,

0 commit comments

Comments
 (0)