List a section's posts anywhere in Stati

The newest posts on the home page from one line of front matter, and prev/next links without a hook.

SNP 145 #1B3 Filed stati eta

yaml
---
# site/index.md
title: Home
collection: /blog
---
eta
<ul>
  <% for (const post of (stati.collection?.recentPages ?? []).slice(0, 5)) { %>
    <li>
      <a href="<%= post.url %>"><%= post.frontMatter.title %></a>
      <% if (post.publishedAt) { %>
        <time datetime="<%= post.publishedAt.toISOString() %>"><%= post.publishedAt.toISOString().slice(0, 10) %></time>
      <% } %>
    </li>
  <% } %>
</ul>
  • collection: /blog (or blog, or /blog/) gives any page the blog's collection. Without it, an index page reads its own folder's and every other page the one it sits in.
  • The entries are page models, not template data. The title is post.frontMatter.title, as there's no post.title, and post.publishedAt is a Date, while stati.page.publishedAt is the string from the front matter.
  • recentPages is newest first, and pages without a date come last. It includes every page under the folder, subfolders too.
  • Stati notes that the page read the collection, so a new, deleted or retitled post rebuilds the home page as well. A change to a post's body doesn't.

The posts read their own folder's collection, so prev/next links need no hook either:

eta
<%
  const posts = stati.collection?.recentPages ?? [];
  const i = posts.findIndex((post) => post.url === stati.page.url);
  const newer = i > 0 ? posts[i - 1] : undefined;
  const older = i >= 0 ? posts[i + 1] : undefined;
%>
<% if (newer) { %><a href="<%= newer.url %>" rel="prev"><%= newer.frontMatter.title %></a><% } %>
<% if (older) { %><a href="<%= older.url %>" rel="next"><%= older.frontMatter.title %></a><% } %>

In the Stati docs: Which collection a page reads, Collection data and What rebuilds a listing.