{{- /* The most viewed posts as a row of cards that scrolls sideways, from data/pageviews.json (written by hugo-writer or scripts/ga4_pageviews.py). A card shows the post's featured image and title, or just the title, larger, when it has no image (or the image doesn't load). Copy to layouts/partials/ pageviews-top.html of your site and call it, e.g. on the home page: {{ partial "pageviews-top.html" . }} {{ partial "pageviews-top.html" (dict "count" 10 "days" 120 "exclude" .) }} count how many (default 10) days only counts taken in the last N days, so a post read a lot long ago doesn't stay on top; 0 for all (default 0) min fewer views than this are left out (default params.pageviews.min, or 1) exclude a page to leave out, e.g. the one it is shown on title the heading (default 熱門文章 / Popular posts; "" for none) views true to show each post's views (default false) < and > buttons page through the row (a few lines of JavaScript; without it, the row still scrolls). The featured image is the post's cover.image (PaperMod) or its first images entry (Hugo's own): a full URL, a file in the post's folder, or a path from the site root. Posts are those of site.MainSections; keys are matched the way pageviews.html does. Shows nothing without data/pageviews.json, when no post has a count, or with popular_posts = false in .hugo-writer.toml (hugo-writer's switch; where they show is up to the templates calling this). Colors follow PaperMod's variables when there. */ -}} {{- $args := cond (reflect.IsMap .) . dict }} {{- $writer := dict }} {{- if fileExists ".hugo-writer.toml" }} {{- $writer = resources.FromString "hugo-writer.toml" (os.ReadFile ".hugo-writer.toml") | transform.Unmarshal }} {{- end }} {{- if ne (index $writer "popular_posts") false }} {{- with site.Data.pageviews }} {{- $pages := .pages | default dict }} {{- $count := $args.count | default 10 }} {{- $days := $args.days | default 0 }} {{- $min := 1 }} {{- with site.Params.pageviews }}{{ $min = .min | default 1 }}{{ end }} {{- with $args.min }}{{ $min = . }}{{ end }} {{- $exclude := "" }} {{- with $args.exclude }}{{ $exclude = .RelPermalink }}{{ end }} {{- $rows := slice }} {{- range where site.RegularPages "Section" "in" site.MainSections }} {{- $page := . }} {{- $key := .RelPermalink | lower | strings.TrimSuffix "index.html" | strings.TrimSuffix ".html" | strings.TrimRight "/" | default "/" }} {{- with index $pages $key }} {{- $at := time .at }} {{- $fresh := or (not $days) (le (sub now.Unix $at.Unix) (mul $days 86400)) }} {{- if and $fresh (ge .views $min) (ne $page.RelPermalink $exclude) }} {{- $rows = $rows | append (dict "page" $page "views" .views) }} {{- end }} {{- end }} {{- end }} {{- with $rows }} {{- $zh := hasPrefix (lower site.Language.Lang) "zh" }} {{- $title := cond (isset $args "title") $args.title (cond $zh "熱門文章" "Popular posts") }}
{{- with $title }}

{{ . }}

{{ end }}
{{- range first $count (sort . "views" "desc") }} {{- $page := .page }} {{- $img := "" }} {{- with .page.Params.cover }}{{ with .image }}{{ $img = . }}{{ end }}{{ end }} {{- if not $img }}{{ with .page.Params.images }}{{ $img = cond (reflect.IsSlice .) (index . 0) . }}{{ end }}{{ end }} {{- $src := "" }} {{- with $img }} {{- if or (hasPrefix . "http://") (hasPrefix . "https://") (hasPrefix . "/") }}{{ $src = . }} {{- else }}{{ with $page.Resources.Get . }}{{ $src = .RelPermalink }}{{ end }}{{ end }} {{- end }} {{- with $src }}{{ end -}} {{ .page.LinkTitle }} {{- if $args.views }}{{ lang.FormatNumber 0 .views }}{{ end -}} {{- end }}
{{- end }} {{- end }} {{- end }}