Skip to content

Commit

Permalink
perf: calculate Index, HasPrev, HasNext for hooks in advance (#58)
Browse files Browse the repository at this point in the history
perf: avoid using merge function to generate context for hooks
  • Loading branch information
razonyang committed Jun 14, 2024
1 parent 9a7d87f commit 8541aa8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 9 additions & 1 deletion layouts/partials/hugopress/functions/modules.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
{{- $nonWeighted = $nonWeighted | append . }}
{{- end }}
{{- end }}
{{- $modules = $weighted | append $nonWeighted }}
{{- $total := len . }}
{{- range $i, $mod := $weighted | append $nonWeighted }}
{{- $mod = merge $mod (dict
"_index" $i
"_hasPrev" (ne $i 0)
"_hasNext" (lt $i (sub $total 1)))
}}
{{- $modules = $modules | append $mod }}
{{- end }}
{{ end }}
{{- return $modules }}
8 changes: 3 additions & 5 deletions layouts/partials/hugopress/functions/render-hooks.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{{- $name := .Name }}
{{- $page := .Page }}
{{- $modules := partialCached "hugopress/functions/modules" $name $name }}
{{- $idx := 0 }}
{{- $total := len $modules }}
{{- range $modules }}
{{- $ctx := dict
"HasPrev" (ne $idx 0)
"HasNext" (lt $idx (sub $total 1))
"Index" $idx
"Page" $page
"Total" $total
"HasPrev" ._hasPrev
"HasNext" ._hasNext
"Index" ._index
}}
{{- if site.Params.hugopress.debug }}
{{- warnf "[hugopress] [%s] rendering hook: %q." .module .name }}
Expand All @@ -27,5 +26,4 @@
{{- else }}
{{- partial .partial $ctx }}
{{- end }}
{{- $idx = add $idx 1 }}
{{- end }}

0 comments on commit 8541aa8

Please sign in to comment.