{{- /*
recommended-posts.html (partial): Renders links styled as card of pages marked as recommended.
Features:
- Displays a maximum of 4 recommended pages, selected randomly.
- Each card includes:
- A "New" indicator if the recommended item was published
within 7 days of the site build.
- Metadata: Shows series (if any) and the first category (if any)
of the recommended item.
- Decorative Accent: Displays the first letter of the recommended
item's title as a large, stylized background element.
Conditions for Display:
- At least one page must have `recommended = true` in its frontmatter.
- This section is displayed on all content pages, *except* if the
current page belongs to a series and is *not* the last part of that series
(determined by `weight`).
- The current page is always excluded from the list of recommendations.
To-do:
1. Exclude the current page with another method (.Permalink?) to avoid
exclusions if pages have the same title
2. DRY: add the card rendering to a partial (duplicated code with paginator)
Repo: https://github.com/oxypteros/alpha
*/ -}}
{{- $currentPage := . }}
{{- $recommendedPages := where site.RegularPages "Params.recommended" true }}
{{- /* Exclude the current page from recommendations */}}
{{- $recommendedPages = where $recommendedPages "Params.title" "ne" $currentPage.Title }}
{{- /* Number of random pages to pick */}}
{{- $numPages := 4 }}
{{- /* Get a randomized sample, but don’t exceed the $numPages */}}
{{- $sampleSize := math.Min (len $recommendedPages) $numPages }}
{{- /* Shuffle our sample */}}
{{- $randomPages := first $sampleSize ($recommendedPages | shuffle) }}
{{- $currentSeries := .Page.Params.series }}
{{- $currentWeight := .Page.Params.weight | int }}
{{- $seriesPages:= where site.RegularPages "Params.series" $currentSeries }}
{{- /* Find the page with the biggest weight */}}
{{- $maxPage := index (sort $seriesPages "Params.weight" "desc") 0 }}
{{- $maxWeight := $maxPage.Params.weight | int }}
{{/* Show recommended posts only if the page is not part of a series or is the last part of one. */}}
{{ if or (eq $currentSeries "") (eq $currentWeight $maxWeight) }}
{{- if gt (len $randomPages) 0 }}
{{ i18n "RecommendedPosts" . }}
{{- range $randomPages }}
{{- /* Find the first letter of the title */}}
{{- $titleAccent := substr .Title 0 1 }}
{{- /* Display only the first category term of the taxonomy */}}
{{- $category := "" -}}
{{- with .GetTerms "categories" -}}
{{- if ge (len .) 1 -}}
{{- $category = (index . 0).LinkTitle -}}
{{- end -}}
{{- end -}}
{{- /* Display 'new' badge only if `date` < 7 days from last build */}}
{{- $new := gt .Date (now.AddDate 0 0 -7) }}
{{- if $new }}
{{- $date := .Date | time.Format "2006-01-02T15:04:05-07:00" }}