Rista

Tags and other groupings

Rista has no notion of a tag. A tag page is a dynamic route whose static_params lists the tags. The same two steps work for categories, authors or years.

One module for the posts

Put the posts in a module that every page imports, so sorting and grouping live in one place:

# src/posts.rb
POSTS = import_glob("./routes/blog/_posts/*.md").values.sort_by { it::Frontmatter.fetch(:date) }.freeze

module Default
  def self.all = POSTS
  def self.tags = POSTS.flat_map { it::Frontmatter.fetch(:tags, []) }.uniq.sort
  def self.tagged(tag) = POSTS.select { it::Frontmatter.fetch(:tags, []).include?(tag) }
end

One route per grouping

-# src/routes/tags/[tag]/+page.haml
:ruby
  Posts = import("/posts")

  def self.static_params
    Posts.tags.map { {tag: it} }
  end

:ruby
  tag = $request.params.fetch(:tag)

%title= "Posts tagged #{tag}"
%h1= "Posts tagged #{tag}"
= Posts.tagged(tag).map do |post|
  %p= post::Frontmatter.fetch(:title)

Any page can list the tags, because Posts.tags is a plain method. This site lists them beside the blog index, with the number of posts of each:

-# src/components/PostIndex.haml
%aside
  %ul
    = Posts.tags.map do |tag|
      %li
        %a{ href: "/tags/#{tag}", rel: "tag" }>= "##{tag}"
        = "(#{Posts.tagged(tag).length})"

Tags go into the URL as written, so use URL-safe values such as ruby or static-sites. They are case-sensitive.

What this site builds this way

All of these are routes and src/posts.rb, with no gem code:

FeatureRoute
Tag pagestags/[tag]/+page.haml
The list of tags beside the blogcomponents/PostIndex.haml
Year archivesblog/[year]/+page.haml
Month archivesblog/[year]/[month]/+page.haml
Previous and next postthe post page, from the post’s index in Posts.all
Cover imagesimport_glob of _covers/*.jpg in three widths, matched to a post by its slug

Pagination

A paginated list is one more dynamic route, whose param is the page number. This site’s blog index shows 20 posts per page: /blog is page 1, and /blog/page/2 appears with the 21st post.

The posts module slices the list:

# src/posts.rb
PER_PAGE = 20

def self.page_count = (POSTS.length.to_f / PER_PAGE).ceil
def self.page(number) = POSTS.reverse.slice((number - 1) * PER_PAGE, PER_PAGE)
def self.page_path(number) = (number == 1) ? "/blog" : "/blog/page/#{number}"

The route lists the pages after the first, so page 1 keeps one URL:

-# src/routes/blog/page/[number]/+page.haml
:ruby
  PostIndex = import("/components/PostIndex.haml")
  Posts = import("/posts")

  def self.static_params
    (2..Posts.page_count).map { {number: it} }
  end

%PostIndex{ page: Integer($request.params.fetch(:number)) }

src/routes/blog/+page.haml renders the same component with page: 1, so the two routes cannot drift apart.

The links between the pages come from src/components/Pagination.haml. It takes the numbers and a callable that turns a page number into a path:

%Pagination{ page: $page, total_pages: Posts.page_count, total: Posts.all.length, per_page: Posts::PER_PAGE, path: Posts.method(:page_path) }

It shows the first and last page, a window around the current one, and a gap where pages are left out. With one page it renders nothing. The arithmetic is in src/pagination.rb, which knows nothing about posts, so a tag page or an archive can use the same component with its own route.

Page numbers are paths because a static site cannot read a query string. /blog/2 would not work here, since /blog/[year] already matches it.