underdot-collections
v2.0.1
Published
Page collections for Underdot
Readme
underdot-collections
Lists the pages below a directory as a global every Underdot template can read, and gives templates a helper that reads a page's rendered body.
Install
npm install underdot-collectionsimport type { Configuration } from 'underdot';
import { collections } from 'underdot-collections';
import { ejs } from 'underdot-ejs';
export default {
plugins: [ejs(), collections({ posts: 'posts' })],
} satisfies Configuration;Collections
Each collection defines a global, named by its key, listing every page below its directory at any depth. The directory's own page is left out.
- Each item is the page's frontmatter plus
_url, the page's URL. - The list is in the order of the pages' source paths.
- The list and its items are frozen, so sort a copy, as
toSortedmakes.
<% for (const post of posts.toSorted((a, b) => b.date - a.date)) { %>
<a href="<%= post._url %>"><%= post.title %></a>
<% } %>pageBody
pageBody(url) returns the rendered body of the page at the URL, before any template wrapped it. A template calls it, and a page body calling it is an error.
<%- pageBody(post._url) %>Options
The factory takes one object. Each key is a collection's name, and each value a directory under the source root, written without a leading or trailing slash. Called with no object, it defines no collection and registers pageBody alone.
q-docs/specs/collections.md holds every rule the plugin commits to.
