Skip to content

Commit

Permalink
internal: add GetLatestMajorVersion banner in documentation HTML
Browse files Browse the repository at this point in the history
This change creates the HTML and CSS changes necessary to show the
latest major version banner.

before (desktop): https://i.imgur.com/YaVw9c8.png
after (desktop): https://i.imgur.com/3XVQPlT.png
before (mobile): https://i.imgur.com/gj54j1o.png
after (mobile): https://i.imgur.com/Xl68EjN.png

Fixes golang/go#37765

Change-Id: I60d6818385c986f9d1ab4dcc8ff663da0e9fe3a3
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/251817
Reviewed-by: Julie Qiu <julie@golang.org>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
  • Loading branch information
Miguel Acero committed Sep 1, 2020
1 parent b008236 commit a611316
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
17 changes: 17 additions & 0 deletions content/static/css/stylesheet.css
Expand Up @@ -423,6 +423,23 @@ code {
.DetailsHeader {
margin-top: 0.75rem;
}
.DetailsHeader-banner {
align-items: center;
background-color: var(--gray-10);
display: flex;
justify-content: flex-start;
padding-top: 0.1rem;
}
.DetailsHeader-banner--latest {
display: none;
}
.DetailsHeader-infoIcon {
color: var(--gray-3);
flex-shrink: 0;
margin-left: 1rem;
margin-right: 0.7rem;
width: 1.3rem;
}
.DetailsHeader-breadcrumb {
align-items: center;
display: flex;
Expand Down
12 changes: 12 additions & 0 deletions content/static/html/pages/details.tmpl
Expand Up @@ -56,6 +56,18 @@
<a href="{{$header.LatestURL}}">Go to latest</a>
</div>
</div>
<div class="DetailsHeader-banner $$GODISCOVERY_LATESTMAJORCLASS$$">
<svg class="DetailsHeader-infoIcon" fill="currentcolor" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 426.667 426.667" style="enable-background:new 0 0 426.667 426.667;" xml:space="preserve">
<rect x="192" y="192" width="42.667" height="128"/>
<path d="M213.333,0C95.467,0,0,95.467,0,213.333s95.467,213.333,213.333,213.333S426.667,331.2,426.667,213.333
S331.2,0,213.333,0z M213.333,384c-94.08,0-170.667-76.587-170.667-170.667S119.253,42.667,213.333,42.667
S384,119.253,384,213.333S307.413,384,213.333,384z"/>
<rect x="192" y="106.667" width="42.667" height="42.667"/>
</svg>
<p>
The latest major version is <a href="/$$GODISCOVERY_LATESTMAJORVERSIONURL$$">$$GODISCOVERY_LATESTMAJORVERSION$$</a>.
</p>
</div>
<div class="DetailsHeader-infoLabel">
<span class="DetailsHeader-infoLabelTitle">Published:</span>
<strong>{{$header.CommitTime}}</strong>
Expand Down
2 changes: 1 addition & 1 deletion internal/datasource.go
Expand Up @@ -15,7 +15,7 @@ type DataSource interface {
// See the internal/postgres package for further documentation of these
// methods, particularly as they pertain to the main postgres implementation.

// GetLatestMajorVersion returns the latest major version of a module path.
// GetLatestMajorVersion returns the latest major version of a series path.
GetLatestMajorVersion(ctx context.Context, seriesPath string) (_ string, err error)
// GetUnitMeta returns information about a path.
GetUnitMeta(ctx context.Context, path, requestedModulePath, requestedVersion string) (_ *UnitMeta, err error)
Expand Down
6 changes: 5 additions & 1 deletion internal/middleware/latestversion.go
Expand Up @@ -60,6 +60,10 @@ func LatestVersions(latestMinor latestMinorFunc, latestMajor latestMajorFunc) Mi
latestMinorClass += "--goToLatest"
}
latestMajorVersion := latestMajor(r.Context(), seriesPath)
latestMajorVersionText := latestMajorVersion
if len(latestMajorVersionText) > 0 {
latestMajorVersionText = latestMajorVersionText[1:]
}
latestMajorClass := ""
// If the latest major version is the same as the major version of the current
// module path, it is currently the latest version so we don't show the banner.
Expand All @@ -71,7 +75,7 @@ func LatestVersions(latestMinor latestMinorFunc, latestMajor latestMajorFunc) Mi
body = bytes.ReplaceAll(body, []byte(latestMinorClassPlaceholder), []byte(latestMinorClass))
body = bytes.ReplaceAll(body, []byte(LatestMinorVersionPlaceholder), []byte(latestMinorVersion))
body = bytes.ReplaceAll(body, []byte(latestMajorClassPlaceholder), []byte(latestMajorClass))
body = bytes.ReplaceAll(body, []byte(LatestMajorVersionPlaceholder), []byte(latestMajorVersion))
body = bytes.ReplaceAll(body, []byte(LatestMajorVersionPlaceholder), []byte(latestMajorVersionText))
body = bytes.ReplaceAll(body, []byte(LatestMajorVersionURL), []byte(seriesPath+latestMajorVersion))
}
if _, err := w.Write(body); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/middleware/latestversion_test.go
Expand Up @@ -174,14 +174,14 @@ func TestLatestMajorVersion(t *testing.T) {
<div class="DetailsHeader-banner$$GODISCOVERY_LATESTMAJORCLASS$$">
data-version="v1.0.0" data-mpath="foo.com/bar" data-ppath="foo.com/bar/far" data-pagetype="pkg">
<p>
The latest major version is available at <a href="/$$GODISCOVERY_LATESTMAJORVERSIONURL$$">$$GODISCOVERY_LATESTMAJORVERSIONURL$$</a>
The latest major version is <a href="/$$GODISCOVERY_LATESTMAJORVERSIONURL$$">$$GODISCOVERY_LATESTMAJORVERSION$$</a>.
</p>
</div>`,
want: `
<div class="DetailsHeader-banner">
data-version="v1.0.0" data-mpath="foo.com/bar" data-ppath="foo.com/bar/far" data-pagetype="pkg">
<p>
The latest major version is available at <a href="/foo.com/bar/v3">foo.com/bar/v3</a>
The latest major version is <a href="/foo.com/bar/v3">v3</a>.
</p>
</div>`,
},
Expand All @@ -197,14 +197,14 @@ func TestLatestMajorVersion(t *testing.T) {
<div class="DetailsHeader-banner$$GODISCOVERY_LATESTMAJORCLASS$$">
data-version="v3.0.0" data-mpath="foo.com/bar/v3" data-ppath="foo.com/bar/far" data-pagetype="pkg">
<p>
The latest major version is available at <a href="/$$GODISCOVERY_LATESTMAJORVERSIONURL$$">$$GODISCOVERY_LATESTMAJORVERSIONURL$$</a>
The latest major version is <a href="/$$GODISCOVERY_LATESTMAJORVERSIONURL$$">$$GODISCOVERY_LATESTMAJORVERSION$$</a>.
</p>
</div>`,
want: `
<div class="DetailsHeader-banner DetailsHeader-banner--latest">
data-version="v3.0.0" data-mpath="foo.com/bar/v3" data-ppath="foo.com/bar/far" data-pagetype="pkg">
<p>
The latest major version is available at <a href="/foo.com/bar/v3">foo.com/bar/v3</a>
The latest major version is <a href="/foo.com/bar/v3">v3</a>.
</p>
</div>`,
},
Expand Down

0 comments on commit a611316

Please sign in to comment.