Skip to content

Commit

Permalink
Merge pull request #295 from gethinode/develop
Browse files Browse the repository at this point in the history
Add versioning support
  • Loading branch information
markdumay committed Jun 16, 2023
2 parents df307be + a0987e3 commit 849d481
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 8 deletions.
2 changes: 2 additions & 0 deletions i18n/en.yaml
Expand Up @@ -89,6 +89,8 @@
translation: "Link"
- id: code
translation: "Code"
- id: latest
translation: "latest"

# 404 page
- id: pageNotFound
Expand Down
2 changes: 2 additions & 0 deletions i18n/nl.yaml
Expand Up @@ -79,6 +79,8 @@
translation: "Link"
- id: code
translation: "Code"
- id: latest
translation: "meest recente"

# 404 page
- id: pageNotFound
Expand Down
5 changes: 3 additions & 2 deletions layouts/_default/baseof.html
@@ -1,6 +1,7 @@
{{- $section := .Section }}
{{- with partial "utilities/GetMenu" . }}{{ $.Scratch.Set "sidebar" . }}{{ end -}}
{{ $.Scratch.Set "version" (site.Param (printf "%s.version" .Section)) }}
{{- $version := partial "utilities/GetVersion.html" (dict "page" . "base" true) -}}
{{- $.Scratch.Set "version" $version -}}
{{ with partial "utilities/GetMenu" (dict "page" . "version" $version) }}{{ $.Scratch.Set "sidebar" . }}{{ end }}

<!doctype html>
<html lang="{{ .Site.Language.Lang }}" class="no-js">
Expand Down
53 changes: 52 additions & 1 deletion layouts/partials/assets/navbar.html
Expand Up @@ -48,6 +48,39 @@
</li>
{{- end -}}

<!-- Inline partial to render the version switcher -->
{{- define "partials/navbar-versions.html" -}}
{{- $size := .size -}}
{{- $collapsed := .collapsed -}}
{{- $page := .page -}}
{{- $list := site.Params.docs.releases -}}
{{- $version := partial "utilities/GetVersion.html" (dict "page" $page) -}}

<li class="nav-item dropdown {{ if $collapsed }}d-{{ $size }}-none{{ else }}d-none d-{{ $size }}-block{{ end }}">
<a class="nav-link dropdown-toggle" href="#!" role="button" data-bs-toggle="dropdown" aria-expanded="false" id="navbar-version-switch">
<span class="d-md-none"></span>{{ $version }}
</a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbar-version-switch">
{{- range $index, $item := $list -}}
{{- $active := eq $item.label $version -}}
{{ if $item.url }}
<li>
<a class="dropdown-item{{ if $collapsed }} switch-mode-collapsed{{ end }}{{ if $active }} active{{ end }}" href="{{ $item.url }}">{{ $item.label }}
{{ if $item.latest }} (T "latest"){{ end }}
{{ if $active }}
<span class="position-absolute end-0 me-3">{{- partial "assets/icon.html" (dict "icon" "fas check") }}</span>
{{ end }}
</a>
</li>
{{ else }}
{{ if gt $index 0}}<li><hr class="dropdown-divider"></li>{{ end }}
<li><span class="dropdown-header fs-6">{{ $item.label }}</span></li>
{{ end }}
{{- end -}}
</ul>
</li>
{{- end -}}

{{- $supportedFlags := slice "true" "false" -}}

{{- $id := printf "navbar-collapse-%d" 0 -}}
Expand Down Expand Up @@ -112,6 +145,12 @@
{{- end -}}
{{- end -}}

{{- $enableVersions := false -}}
{{ $list := site.Params.docs.releases }}
{{ if $list }}
{{- $enableVersions = gt (len $list ) 1 -}}
{{ end }}

{{- $enableLanguage := or $page.IsTranslated site.IsMultiLingual -}}

{{- $logo := site.Params.navigation.logo -}}
Expand Down Expand Up @@ -185,7 +224,7 @@
{{- end -}}

<!-- Insert divider if applicable -->
{{- if or $enableLanguage $enableDarkMode -}}
{{- if or $enableLanguage $enableVersions -}}
<li class="nav-item py-2 py-md-1 col-12 col-md-auto d-none d-{{ $size }}-block">
<div class="vr d-none d-md-flex h-100 mx-md-2"></div>
</li>
Expand Down Expand Up @@ -216,8 +255,20 @@
</li>
{{- end -}}

<!-- Insert version switcher -->
{{- if $enableVersions -}}
{{- partial "partials/navbar-versions.html" (dict "page" $page "size" $size "collapsed" true ) -}}
{{- partial "partials/navbar-versions.html" (dict "page" $page "size" $size "collapsed" false) -}}
{{- end -}}

<!-- Insert color mode switcher -->
{{- if $enableDarkMode -}}
<!-- Insert divider -->
<li class="nav-item py-2 py-md-1 col-12 col-md-auto d-none d-{{ $size }}-block">
<div class="vr d-none d-md-flex h-100 mx-md-2"></div>
</li>
<li><hr class="dropdown-divider-bg"></li>

{{- partial "partials/navbar-mode.html" (dict "size" $size "collapsed" true) -}}
{{- partial "partials/navbar-mode.html" (dict "size" $size "collapsed" false) -}}
{{- end -}}
Expand Down
10 changes: 8 additions & 2 deletions layouts/partials/utilities/GetMenu.html
@@ -1,6 +1,12 @@
{{- $page := .page -}}
{{- $version := .version -}}

{{- $menu := "" -}}
{{- if .IsPage -}}
{{- $menu = index .Site.Data .Section -}}
{{- if $page.IsPage -}}
{{- $menu = index site.Data (printf "%s-%s" $page.Section $version) -}}
{{ if not $menu }}
{{ $menu = index site.Data $page.Section }}
{{ end }}
{{- end -}}

{{- return $menu -}}
34 changes: 34 additions & 0 deletions layouts/partials/utilities/GetVersion.html
@@ -0,0 +1,34 @@
<!--
Retrieve the docs version label of the current page. The path of the page's file is matched with the url of the
defined docs releases in the site's parameters. When no match is found it returns the latest label instead.
-->

{{- $page := .page -}}
{{- $base := .base -}}
{{- $version := "" -}}

{{- $docsPath := strings.TrimPrefix "/" site.Params.docs.contentPath -}}
{{- if and $page.File (strings.HasPrefix $page.File.Dir $docsPath) -}}
{{- $currentPath := strings.TrimPrefix $docsPath $page.File.Dir -}}
{{- $currentVersion := index (split $currentPath "/") 0 -}}
{{- $versionPath := path.Join site.Params.docs.contentPath $currentVersion -}}

{{- $list := site.Params.docs.releases -}}
{{- range $index, $item := $list -}}
{{- $url := strings.TrimSuffix "/" $item.url -}}
{{- if eq $url $versionPath -}}
{{- if $base }}{{ $version = path.Base $item.url }}{{ else }}{{ $version = $item.label }}{{ end -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{- if not $version -}}
{{- $list := site.Params.docs.releases -}}
{{- range $item := $list -}}
{{- if $item.latest -}}
{{- if $base }}{{ $version = path.Base $item.url }}{{ else }}{{ $version = $item.label }}{{ end -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{- return $version -}}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@gethinode/hinode",
"version": "0.14.7",
"version": "0.15.0-alpha",
"description": "Hinode is a clean documentation and blog theme for Hugo, an open-source static site generator",
"keywords": [
"hugo",
Expand Down

0 comments on commit 849d481

Please sign in to comment.