1+ /**
2+ * a tax is defined as a collection of values, which hold a collection of pages
3+ * a tax may be 'tags', a value may be 'cats', and a page may be 'cats.md'
4+ *
5+ * a single tax can be nominated as a differentiator, which means pages of a different
6+ * tax will be grouped by the differentiator
7+ *
8+ * taxes: [tax,value]
9+ * taxesPaged: variation on taxes, see below
10+ * taxesDiffer: [tax,value, differentiator]
11+ * nestedTax: {tax: {value: [page]}}
12+ *
13+ * if eleventy supported double-layered pagination, all of this wouldn't be needed
14+ */
15+
116import { createRequire } from "node:module" ;
217const require = createRequire ( import . meta. url ) ;
318const collectionControl = require ( "../../src/_data/collectionsControl.json" ) ;
@@ -61,6 +76,13 @@ export function taxes(eleventyConfig) {
6176 return unique ;
6277 } ) ;
6378
79+ /**
80+ * The goal of this is to paginate taxes with the following rules:
81+ * - if it isn't a differentiator, it should not be paginated
82+ * practically, this means only in: blog, garden, ... is paginated
83+ * - 30 items per page
84+ * - if the last page has less than 5 items, it should be merged with the previous page
85+ */
6486 // biome-ignore lint/complexity/useArrowFunction: <explanation>
6587 eleventyConfig . addCollection ( "taxesPaged" , function ( collectionApi ) {
6688 const taxAndValues = standard ( collectionApi ) ;
@@ -73,7 +95,10 @@ export function taxes(eleventyConfig) {
7395 const [ taxonomy , value ] = key . split ( "/./" ) ;
7496 let totalItems = taxAndValues [ key ] ;
7597 let pageNumber = 0 ;
76-
98+ let maxPage = Math . ceil ( totalItems / maxItemsPerPage ) - 1 ;
99+ if ( totalItems % maxItemsPerPage < minItemsForLastPage ) {
100+ maxPage -- ;
101+ }
77102 if ( collectionControl [ taxonomy ] . mode !== "differentiator" ) {
78103 paginatedResults . push ( {
79104 t : taxonomy ,
@@ -82,6 +107,7 @@ export function taxes(eleventyConfig) {
82107 itemsInPage : totalItems ,
83108 hasNext : false ,
84109 hasPrev : false ,
110+ maxPage,
85111 } ) ;
86112 continue ;
87113 }
@@ -103,6 +129,7 @@ export function taxes(eleventyConfig) {
103129 itemsInPage : itemsInPage ,
104130 hasNext : totalItems > itemsInPage ,
105131 hasPrev : pageNumber > 0 ,
132+ maxPage,
106133 } ) ;
107134
108135 totalItems -= itemsInPage ;
0 commit comments