Posts: Improve PageIndex datasource, pagination. Add 'post' pagetype and support published_date field #144
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Improvements to support blog post pages.
postpage type and support apublished_dateon pages./posts/then/posts/2/,/posts/3/etc. for subsequent pagination.Extra Datasource Configuration
You can pass extra options through to the datasource in your Kibble.json, for example:
{ "routes": [ { "name": "postIndex", "datasource": "PageIndex", "urlPath": "/posts/:index/", "options": { "pageTypes": ["post"], "sortBy": ["published_date:desc", "title:asc"] }, "pageSize": 10 }, ] }The PageIndex source supports filtering down to particular page types to include in the index listing, as well as sorting criteria.
Pagination
You can configure a different path for page 1 of paginated index by using
firstPageUrlPath{ "routes": [ { "name": "pageIndex", "datasource": "PageIndex", "firstPageUrlPath": "/pages/", "urlPath": "/pages/:index/", "pageSize": 10 } ] }Internally, pagination has been refactored so that it's now a standalone concern that can easily be added to any data source that needs it by calling
ctx.Paginate(len(items))on the render context. This returns a list of pages that can be iterated over, and calculates the slice indexes for each page.Caveats
Currently, configuring routes for different kinds of pages isn't supported.
I had initially added the
pageTypesfilter to the Page datasource, so that you could define different routes to render specific types of pages — blogs on /posts/, curated pages on /curated/ and everything on /pages/{ "routes": [ { "name": "postItem", "datasource": "Page" "urlPath": "/posts/:slug", "options": { "pageTypes": ["post"] }, }, { "name": "curatedPageItem", "datasource": "Page" "urlPath": "/curated/:slug", "options": { "pageTypes": ["curated"] }, }, { "name": "pageItem", "datasource": "Page" "urlPath": "/page/:slug" }, ] }While this generated the site correctly, it exposed an issue with the way Kibble's link generation works: the routeTo functions will just pick the first route whose datasource claims to handle a particular entity/slug. With the above config, every link would be generated as
/posts/....Supporting this will take a bit more refactoring, such as instantiating each datasource per-route so that methods like IsSlugMatch, GetRouteForSlug, and GetRouteForEntity can have access to the datasource options