Guides

Navigation for a sidebar

Declare how a collection groups and orders its entries, and read them arranged that way in one call, for a sidebar or a table of contents.

Navigation for a sidebar

A sidebar of docs, or the chapters of a guide, needs every published entry of a collection, grouped and in order. Declare that order once on the collection, and every site reads it arranged in one call.

Declare it

A collection's navigation names two fields of its frontmatter, and the order of the groups:

"navigation": {
  "groupBy": "section",
  "groups": ["start", "guides", "concepts", "reference"],
  "orderBy": "order"
}
FieldWhat it is
groupBya string or enum field, whose value names the group of an entry
groupsthe groups first, in this order; for an enum, values of it. Without it, an enum's own order, or the names in order
orderBya number field, which orders the entries of a group, lowest first

Each group lists its entries by orderBy, then by title in the locale, then by slug; an entry without a value of orderBy comes after those with one. Entries with no value of groupBy make a last group, named null. A collection without navigation is a single group, by title.

Set it with set_collection, with the whole collection as get_structure gave it plus navigation. A collection from the docs preset has it already: it groups by section and orders by order. A field of another type, or groups without groupBy, is refused at its path, with the fix.

Read it

The navigation holds the published entries of one locale, the site's default unless you name another:

{
  "outcome": "ok",
  "collection": "docs",
  "locale": "en",
  "groups": [
    {
      "name": "start",
      "entries": [
        { "slug": "introduction", "title": "What is Hollware", "description": "A headless CMS on Cloudflare Workers…", "path": "/docs/introduction/", "order": 1 }
      ]
    }
  ]
}

Each entry has its slug, its title (its slug when it has none), its description or null, its path by the route of the collection, or null without one, and its order or null. createHollware(…).getNavigation answers the same without outcome.

FromCall
A service bindingenv.HOLLWARE.getNavigation('my-site', 'docs', { locale: 'en' })
HTTP, with a read tokenGET /v1/docs/navigation?locale=en
@hollware/client, @hollware/reactcreateHollware(…).getNavigation('docs', { locale: 'en' })
@hollware/nuxtuseHollwareNavigation('docs', { locale: 'en' }), served by /_hollware/navigation/docs
@hollware/vueuseHollwareNavigation('docs', { locale: 'en' }), which gives { data, error, pending }
@hollware/astroawait hollwareNavigation({ ...options, collection: 'docs', locale: 'en' })

In Nuxt:

<script setup lang="ts">
const { data } = await useHollwareNavigation('docs', { locale: 'en' })
</script>

<template>
  <nav>
    <section v-for="group in data?.groups" :key="group.name ?? ''">
      <h2>{{ group.name }}</h2>
      <NuxtLink v-for="entry in group.entries" :key="entry.slug" :to="entry.path ?? '/'">{{ entry.title }}</NuxtLink>
    </section>
  </nav>
</template>

With cache: 'purge', a Nuxt page that shows the navigation is purged with the list of its collection and locale, so a publication shows in the sidebar too (Caching).

A navigation arranges up to 1,000 published entries of a locale. A larger one is refused (output_too_large, a 422 over HTTP): read it a page at a time with listEntries.

A nested collection

In a nested collection, the navigation is a tree. The groups hold the roots, and each entry has its parent, a slug or null, and its children, in the order the entries were given with move_entry or by dragging in the admin, then by title. path is the whole path, such as /help/billing/refunds/. In a flat collection, parent is null and children is empty.

{ "slug": "billing", "title": "Billing", "path": "/help/billing/", "parent": null, "children": [
  { "slug": "refunds", "title": "Refunds", "path": "/help/billing/refunds/", "parent": "billing", "children": [] }
] }

Two helpers read the tree, from @hollware/client and every package of a framework:

HelperWhat it gives
trailOf(navigation, slug)the entries from the root down to the one with slug: the breadcrumbs
flattenNavigation(navigation)every entry in the order of the sidebar: previous and next

@hollware/react draws the tree with <HollwareTree groups={navigation.groups} current={slug} />, a list in a <nav> whose branch of the current page is open, and no styles of its own. Pass link to render the links with your router.