fix: generate mobile nav data

This commit is contained in:
Xin 2024-12-30 22:38:28 +00:00
parent b01c8ee405
commit f8e02e6151
3 changed files with 53 additions and 7 deletions

View File

@ -0,0 +1,35 @@
{{/* Generate mobile navigation data based on main menu */}}
{{- $context := . -}}
{{- $data := slice -}}
{{- range .Site.Menus.main -}}
{{- if not (eq .Params.type "search") -}}
{{- $title := or (T .Identifier) .Name -}}
{{- $link := .URL -}}
{{- $external := strings.HasPrefix $link "http" -}}
{{- with .PageRef -}}
{{- if hasPrefix . "/" -}}
{{- $link = relLangURL (strings.TrimPrefix "/" .) -}}
{{- end -}}
{{- end -}}
{{- with .Page -}}
{{- $page := . -}}
{{- if and $page.IsSection (eq $page.Type "docs") -}}
{{- $page = (partial "utils/translated-page" (dict "page" $page "lang" site.Language.LanguageCode)) -}}
{{- $sectionData := (partial "components/sidebar/generate-section-data" $page) | unmarshal -}}
{{- $data = $data | append (dict "title" $title "link" $link "items" $sectionData) -}}
{{- else -}}
{{- $data = $data | append (dict "title" $title "link" $link) -}}
{{- end -}}
{{- else -}}
{{/* TODO: handle other cases like external links */}}
{{- $data = $data | append (dict "title" $title "link" $link) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- return ($data | jsonify (dict "noHTMLEscape" true)) -}}

View File

@ -15,20 +15,14 @@
{{- $pageURL := $context.RelPermalink -}}
{{- $data := slice -}}
{{- $dataMobile := slice -}}
{{- $dataMobile := (partialCached "components/sidebar/generate-mobile-data" site.Home site.Home) | unmarshal -}}
{{- if (eq site.Params.page.sidebar.source "data") -}}
{{/* Get sidebar data from Hugo `data` directory */}}
{{- $data = partialCached "components/sidebar/get-section-data" $context $context.Section -}}
{{- $dataMobile = $data -}}
{{- else -}}
{{/* Generate and cache sidebar data in memory */}}
{{- $data = (partialCached "components/sidebar/generate-section-data" $navRoot $navRoot) | unmarshal -}}
{{- $dataMobile = (partialCached "components/sidebar/generate-section-data" site.Home site.Home) | unmarshal -}}
{{- end -}}
{{- if not (hugo.IsProduction) -}}
{{ with $data }}{{ warnf "%v" (debug.Dump .) }}{{ end }}
{{- end -}}
{{/* Cache rendered sidebar */}}

View File

@ -0,0 +1,17 @@
{{/*
Utility to retrieve a translated page given a page and a language code.
If the page is not translated, it returns the original page.
*/}}
{{- $page := .page -}}
{{- $lang := .lang -}}
{{- if $page.IsTranslated -}}
{{- range $page.AllTranslations -}}
{{- if eq .Language.LanguageCode $lang -}}
{{- $page = . -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- return $page -}}