mirror of
https://github.com/imfing/hextra.git
synced 2025-05-13 06:29:03 -04:00
feat: generate sidebar json data for rendering
This commit is contained in:
parent
63f153999e
commit
33f2cf653b
@ -0,0 +1,51 @@
|
||||
{{- $context := . -}}
|
||||
|
||||
{{- $pages := union .RegularPages .Sections -}}
|
||||
{{- $pages = where $pages "Params.sidebar.exclude" "!=" true -}}
|
||||
|
||||
{{- $data := slice -}}
|
||||
|
||||
{{- range $pages.ByWeight -}}
|
||||
{{ $structure := (partial "sidebar/section-walk" .) | unmarshal -}}
|
||||
{{ $data = $data | append $structure -}}
|
||||
{{ end -}}
|
||||
|
||||
{{- define "partials/sidebar/section-walk" -}}
|
||||
{{- with . -}}
|
||||
{
|
||||
"title": "{{ .LinkTitle | default .File.BaseFileName }}",
|
||||
"link": "{{ .RelPermalink }}",
|
||||
"toc": {{ partial "sidebar/section-page-toc" . }},
|
||||
"open": {{ .Params.sidebar.open | default false }}
|
||||
{{- if .IsSection }},
|
||||
"items": [
|
||||
{{ $pages := union .RegularPages .Sections -}}
|
||||
{{ $pages = where $pages "Params.sidebar.exclude" "!=" true -}}
|
||||
{{ range $index, $page := $pages.ByWeight -}}
|
||||
{{ partial "sidebar/section-walk" . }}{{ if not (ge $index (sub (len $pages) 1)) }},{{ end -}}
|
||||
{{ end -}}
|
||||
]
|
||||
{{ end -}}
|
||||
}
|
||||
{{- end }}
|
||||
{{- end -}}
|
||||
|
||||
{{- define "partials/sidebar/section-page-toc" -}}
|
||||
{{/* Get level 2 headings list used mainly for mobile navigation */}}
|
||||
[
|
||||
{{- with .Fragments.Headings -}}
|
||||
{{/* Loop over level 1 headings */}}
|
||||
{{- range . }}
|
||||
{{- with .Headings }}
|
||||
{{ $headings := . }}
|
||||
{{- range $index, $heading := $headings }}
|
||||
{{ $heading.Title | jsonify (dict "noHTMLEscape" true) }}
|
||||
{{- if not (ge $index (sub (len $headings) 1)) }},{{ end -}}
|
||||
{{ end -}}
|
||||
{{- end -}}
|
||||
{{ end -}}
|
||||
{{- end -}}
|
||||
]
|
||||
{{- end -}}
|
||||
|
||||
{{ return ($data | jsonify (dict "noHTMLEscape" true)) }}
|
@ -4,7 +4,7 @@
|
||||
{{- range .data -}}
|
||||
{{- $active := or (eq $pageLink .link) (eq (strings.TrimSuffix "/" $pageLink) .link) -}}
|
||||
{{- $containsPage := hasPrefix $pageLink .link -}}
|
||||
{{- $shouldOpen := or (.open) $containsPage $active | default true -}}
|
||||
{{- $shouldOpen := or (.open) $containsPage $active | default false -}}
|
||||
|
||||
<li class="{{ if $shouldOpen }}open{{ end }}">
|
||||
{{- partial "components/sidebar/item-link" (dict "active" $active "title" .title "link" .link "items" .items) -}}
|
||||
|
@ -1,6 +1,5 @@
|
||||
{{- $items := .items -}}
|
||||
{{- $pageLink := .link -}}
|
||||
{{ $shouldOpen := true }}
|
||||
|
||||
|
||||
<div class="ltr:hx-pr-0 hx-overflow-hidden">
|
||||
@ -9,6 +8,10 @@
|
||||
>
|
||||
{{- range $items }}
|
||||
{{- $active := or (eq $pageLink .link) (eq (strings.TrimSuffix "/" $pageLink) .link) -}}
|
||||
{{- $containsPage := hasPrefix $pageLink .link -}}
|
||||
{{- $shouldOpen := or (.open) $containsPage $active | default false -}}
|
||||
|
||||
|
||||
<li class="hx-flex hx-flex-col {{ if $shouldOpen }}open{{ end }}">
|
||||
{{- partial "components/sidebar/item-link" (dict "active" $active "title" .title "link" .link "items" .items) -}}
|
||||
{{- if .items -}}
|
||||
|
@ -8,8 +8,8 @@
|
||||
{{- $navRoot := cond (eq site.Home.Type "docs") site.Home $context.FirstSection -}}
|
||||
{{- $pageURL := $context.RelPermalink -}}
|
||||
|
||||
{{ $data := partialCached "components/sidebar/get-section-data" $context $context.Section }}
|
||||
|
||||
{{/* {{ $data := partialCached "components/sidebar/get-section-data" $context $context.Section }} */}}
|
||||
{{ $data := partial "components/sidebar/generate-section-data" $navRoot | unmarshal }}
|
||||
|
||||
<div class="mobile-menu-overlay [transition:background-color_1.5s_ease] hx-fixed hx-inset-0 hx-z-10 hx-bg-black/80 dark:hx-bg-black/60 hx-hidden"></div>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user