From 8541aa880c50402489aa017fd79328714c8847d2 Mon Sep 17 00:00:00 2001 From: Razon Yang Date: Fri, 14 Jun 2024 14:02:15 +0800 Subject: [PATCH] perf: calculate Index, HasPrev, HasNext for hooks in advance (#58) perf: avoid using merge function to generate context for hooks --- layouts/partials/hugopress/functions/modules.html | 10 +++++++++- layouts/partials/hugopress/functions/render-hooks.html | 8 +++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/layouts/partials/hugopress/functions/modules.html b/layouts/partials/hugopress/functions/modules.html index cb9c658..9d31ff5 100644 --- a/layouts/partials/hugopress/functions/modules.html +++ b/layouts/partials/hugopress/functions/modules.html @@ -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 }} diff --git a/layouts/partials/hugopress/functions/render-hooks.html b/layouts/partials/hugopress/functions/render-hooks.html index 2626149..9938e2b 100644 --- a/layouts/partials/hugopress/functions/render-hooks.html +++ b/layouts/partials/hugopress/functions/render-hooks.html @@ -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 }} @@ -27,5 +26,4 @@ {{- else }} {{- partial .partial $ctx }} {{- end }} - {{- $idx = add $idx 1 }} {{- end }}