Skip to content

Commit

Permalink
feat: allow to limit the number of tree posts (#196)
Browse files Browse the repository at this point in the history
Allow to limit the number of posts listed per category/section entry

Closes #97
  • Loading branch information
reuixiy committed Jun 12, 2020
1 parent 61da9ae commit 6f3aeba
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
5 changes: 5 additions & 0 deletions config-examples/en/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ uglyURLs = false
# category?
displayPostsCount = true

# Set to a non-negative number to limit
# the number of posts displayed per
# category/section.
limitPostsLimit = -1


######################################
# Tags Page
Expand Down
3 changes: 3 additions & 0 deletions config-examples/zh-cn/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ uglyURLs = false
# 是否显示每一分类下的文章数
displayPostsCount = true

# 设置一个非负数来限制每一分类下展示的文章数
limitPostsLimit = -1


######################################
# 标签页面
Expand Down
9 changes: 6 additions & 3 deletions layouts/partials/pages/tree-categories.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ <h1 class="list-title">{{ .Title | default (.Type | title) }}</h1>
{{ $lastTerm := (delimit (last 1 $categoryTerms) " ") }}
{{ $url := urls.Parse $lastTerm }}
{{ $path := $url.Path }}
{{ $linkTarget := "" }}
{{ with $.Site.GetPage (printf `/categories/%s` $path) }}
{{ $linkTarget = . }}
<p style="padding-left: {{ $padding }}em">
<a href="{{ .RelPermalink }}" class="category-item">{{ .LinkTitle | default .Data.Term | default $path }}</a>
{{ if $.Site.Params.displayPostsCount }}
Expand All @@ -34,6 +36,7 @@ <h1 class="list-title">{{ .Title | default (.Type | title) }}</h1>
{{ end }}
{{ if $.Site.Params.displayPosts }}
{{ $context := . }}
{{ $.Scratch.Delete "pages" }}
{{ range $.Site.RegularPages }}
{{ $page := . }}
{{ with .Param "categories" }}
Expand All @@ -44,12 +47,12 @@ <h1 class="list-title">{{ .Title | default (.Type | title) }}</h1>
{{ end }}
{{ $category := $.Scratch.Get "category" }}
{{ if eq $context $category }}
<p style="padding-left: {{ add $padding (mul 2 1.2) }}em">
<a href="{{ $page.RelPermalink }}" class="category-post">{{ (partial "utils/title.html" (dict "$" $page "title" $page.LinkTitle)).htmlTitle }}</a>
</p>
{{ $.Scratch.Add "pages" (slice $page) }}
{{ end }}
{{ end }}
{{ end }}
{{ $pages := $.Scratch.Get "pages" }}
{{ partial "utils/limit-tree-posts.html" (dict "$" $ "pages" $pages "linkTarget" $linkTarget "padding" $padding) }}
{{ end }}
{{ end }}
</div>
Expand Down
7 changes: 2 additions & 5 deletions layouts/partials/pages/tree-sections.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1 class="list-title">{{ .Title | default (.Type | title) }}</h1>
{{ range $page, $name := $pages }}
{{ $depth := (len (split (strings.TrimPrefix "/" $page) "/")) }}
{{ with $.Site.GetPage $page }}
{{ $linkTarget := . }}
{{ $padding := (mul (mul $depth 2) 1.2) }}
<p style="padding-left: {{ $padding }}em">
<a href="{{ .RelPermalink }}" class="category-item">{{ .LinkTitle | default $name }}</a>
Expand All @@ -34,11 +35,7 @@ <h1 class="list-title">{{ .Title | default (.Type | title) }}</h1>
{{ end }}
{{ end }}
{{ $pages := $.Scratch.Get "pages" }}
{{ range $pages }}
<p style="padding-left: {{ add $padding (mul 2 1.2) }}em">
<a href="{{ .RelPermalink }}" class="category-post">{{ (partial "utils/title.html" (dict "$" . "title" .LinkTitle)).htmlTitle }}</a>
</p>
{{ end }}
{{ partial "utils/limit-tree-posts.html" (dict "$" $ "pages" $pages "linkTarget" $linkTarget "padding" $padding) }}
{{ end }}
{{ end }}
{{ end }}
Expand Down
27 changes: 27 additions & 0 deletions layouts/partials/utils/limit-tree-posts.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{- $ := index . "$" -}}
{{- $pages := .pages -}}
{{- $linkTarget := .linkTarget -}}
{{- $padding := .padding -}}

{{ with $pages }}
{{ $rawPages := . }}
{{ $limit := -1 }}
{{ if isset $.Site.Params "limitpostslimit" }}
{{ $limit = $.Site.Params.limitPostsLimit }}
{{ end }}
{{ if ge $limit 0 }}
{{ $pages = $pages | first $limit -}}
{{ end }}
{{ range $pages }}
<p style="padding-left: {{ add $padding (mul 2 1.2) }}em">
<a href="{{ .RelPermalink }}" class="category-post">{{ (partial "utils/title.html" (dict "$" . "title" .LinkTitle)).htmlTitle }}</a>
</p>
{{ end }}
{{ if and (gt $limit 0) (gt (len $rawPages) $limit) }}
{{ with $linkTarget }}
<p style="padding-left: {{ add $padding (mul 2 1.2) }}em">
<a href="{{ $linkTarget.RelPermalink }}" class="category-post">{{ i18n "readMore" }} »</a>
</p>
{{ end }}
{{ end }}
{{ end }}

0 comments on commit 6f3aeba

Please sign in to comment.