Skip to content

@ogygia/content · Macros

Loaders

import.meta.og.loader.markdown, .folder, .json and .git point a collection at files with one literal path. The build writes the import.meta.glob for you, picks the files each kind reads, and loads each file only when it's read.

On this page

A collection needs a source, and for files in your repo that source is a loader macro. You give it a folder, and the build turns the call into the right import.meta.glob and source builder before your code runs. You write no glob options, no wrapper import and no id function.

The loaders come with @ogygia/content: list its content() plugin beside ogygia() and add /// <reference types="@ogygia/content/types" /> to src/ogygia.d.ts (see Install). Without the plugin, a call fails the build with macro_needs_plugin.

import { content } from '@ogygia/content';

export const posts = content({
  loader: import.meta.og.loader.markdown('../content/posts')
});

// what the build writes in its place:
// __og_markdown(import.meta.glob(['../content/posts/**/*.svx', '../content/posts/**/*.md'], { eager: false }))

Loader kinds

Each loader takes a folder, relative to the module, and reads its own kind of file anywhere below it:

LoaderReadsEntries
markdown(dir, opts?)every .svx and .md filecompiled markdown: frontmatter as data, meta.headings and meta.links, a body and source
folder(dir, opts?)+doc.svx pages and +meta.json section filesmarkdown pages ordered by NN- prefixes, with section labels. What a docs site uses.
json(dir, opts?)every .json filedata only: the file’s object is data, and there’s no body
git(spec, opts?).md files in another git repositorya folder source over a checkout of that repository

Markdown needs the plugin’s markdown option; see Markdown. folder and git entries carry meta.headings and meta.links too, but their meta is typed as empty today, so pass the type to read them: folder<MarkdownMeta>(dir), with MarkdownMeta from @ogygia/content. A JSON file must hold an object (or a module whose only export is one); an array or a number throws content_format_not_object. A file the markdown pipeline didn’t compile, one whose extension isn’t in markdown.extensions, throws content_markdown_not_module or content_markdown_no_metadata.

An argument with glob characters (*, ?, {, [), or one whose last part has a dot (a single file), is used as it is: markdown('../content/docs/**/*.svx') reads only .svx files. That’s the way out when a folder holds files the default would pick up. Brace groups are expanded into a list, because Vite’s dev server and its build match {+doc.svx,+meta.json} differently.

import { content } from '@ogygia/content';

export const posts = content({
  loader: import.meta.og.loader.markdown('../content/posts')
});
export const docs = content({
  loader: import.meta.og.loader.folder('../content/docs')
});
export const authors = content({
  loader: import.meta.og.loader.json('../content/authors')
});
export const notes = content({
  loader: import.meta.og.loader.markdown(
    '../content/notes/**/*.svx'
  )
});

Lazy loading

The glob the build writes is lazy: every file is its own chunk, and get(id) loads only the one it reads. A large corpus on a serverless host stays a small function that loads pages as they’re asked for, rather than one module holding every compiled page.

Reads that list the whole collection (refs(), a nav, a search index, a collection with relations) still load every file once. After that the collection keeps the catalog.

Folder order

folder() reads a folder laid out the way a docs site is: each page is a folder with a +doc.svx in it, and an NN- prefix on a folder sets its place among its siblings.

  • The id is the path with the +doc.svx file name and every NN- prefix taken off.
  • Each entry’s order holds its prefix numbers, so a site’s outline can sort it. A folder without a prefix sorts last.
  • A +meta.json names its folder’s section: { "label": "…" }. The collection’s groups() holds the labels of folders that have a +meta.json. A folder without one isn’t in groups(); a site’s outline labels it with the folder name in title case (data-state becomes Data State).

Prefixes are checked as the collection starts. Once one sibling has a prefix, they all need one, padded to the same width; two siblings can’t share a number. { "ordered": false } in a folder’s +meta.json turns the check off for that folder. A failed check throws content_folder_invalid, listing every folder at fault.

Two pages whose ids match once the prefixes are gone (01-intro and 02-intro) throw content_folder_duplicate_id. Keep every page in a folder of its own. A +doc.svx at the very top of the collection doesn’t fail today: it quietly becomes an entry with the id +doc.svx. Move it into a folder, such as 00-intro/+doc.svx. With a custom page pattern, a path that ends up empty throws content_folder_empty_id. A file the glob matched that is neither a page nor a section file throws content_folder_unexpected_file, so a too-wide glob can’t drag components that sit next to a page into the collection.

Folder options

The second argument of folder() and git() changes the layout it reads:

OptionDefaultWhat it does
page+doc.* or index.*A regular expression for the files that are pages. What it matches is taken off the id, so /\.md$/ keeps the file name as the last part of the id.
meta+meta.jsonA regular expression for section files, or false for none. It wins over page, so /\/index\.md$/ makes an index.md a section file whose frontmatter title labels it.
conventionnumbered()How a file name orders its siblings. numbered() reads NN-; dated() reads a date; or pass your own (below).
formatmarkdownWhat a page file becomes: (raw) => source, for example (raw) => blocks(raw, registry).

markdown() and json() take one option instead, id: (key) => string, to choose ids yourself; see How entry ids are made.

The directory form of folder() reads only +doc.svx and +meta.json, so a layout with other file names passes a glob as well as page.

numbered({ pad, contiguous, duplicates }) tightens or loosens its check: pad: 2 requires two-digit prefixes, contiguous: true requires 00/01 onward with no gaps, and duplicates: 'allow' lets siblings share a number (they then sort by name), for a corpus you didn’t number yourself. dated({ format }) orders by a date prefix ('YYYY-MM-DD' by default; any order of YYYY, MM and DD with separators), oldest first, and takes the date off the id: 2026-08-13-release.md becomes release. dateOf(segment, format?) gets the date back as YYYY-MM-DD from one path segment that starts with the date, so pass it the file name, not the whole path: dateOf(ref.filePath!.split('/').pop()!). It returns null for a segment that doesn’t start with a date. A format without all three parts throws content_dated_format_invalid. The outline shows both in a site.

A convention of your own is an object with three methods: segment(raw) returns { slug, order } for one file or folder name, label(slug) returns a default label, and verify(dir, segments, meta) returns a list of problems with one folder’s siblings (an empty list passes). The Convention type describes it.

content/docs/
  00-start/
    +meta.json          { "label": "Getting started" }
    00-overview/+doc.svx  → id start/overview
    01-install/+doc.svx   → id start/install
  01-guides/
    00-routing/+doc.svx   → id guides/routing
import { content, dated } from '@ogygia/content';

export const blog = content({
  loader: import.meta.og.loader.folder(
    '../content/blog/**/*.md',
    {
      page: /\.md$/,
      meta: /(^|\/)index\.md$/,
      convention: dated()
    }
  )
});

Git sources

git(spec, opts?) builds a collection straight from another git repository, so you can publish its docs with no copy in your repo and no sync script. spec is owner/repo, optionally followed by @ref (a branch or a tag; the default branch otherwise) and :path (a folder in the repository). The repository is read from GitHub.

At build time the plugin makes a shallow checkout of just that folder under node_modules/.ogygia/content/, and the call becomes a folder source over its .md files. opts are folder options. A repository’s docs rarely use +doc.svx folders, so you’ll usually pass page and meta, as below; without them, any .md that isn’t +doc.md or index.md fails with content_folder_unexpected_file.

The first build records the commit it checked out, in node_modules/.ogygia/git/, and later builds reuse that checkout without touching the network, so a host that caches node_modules clones nothing. It also means a branch stays at that commit: delete node_modules/.ogygia/git/ to fetch the branch’s newest commit on the next build. To pin a version on purpose, put a tag in the spec (owner/repo@v2.1.0:docs). A commit sha doesn’t work as a ref today: the first checkout clones with --branch, which takes only a branch or a tag, so the build fails.

A spec that isn’t owner/repo[@ref][:path] throws content_git_spec_invalid. A build that can’t write node_modules/.ogygia throws content_git_cache_unavailable. The machine running the build needs git.

import { content, numbered } from '@ogygia/content';

export const svelte = content({
  loader: import.meta.og.loader.git(
    'sveltejs/svelte@main:documentation/docs',
    {
      page: /\.md$/,
      meta: /(^|\/)index\.md$/,
      convention: numbered({ duplicates: 'allow' })
    }
  )
});

Presets

One option belongs to the macro and is used at build time: preset, the name of a markdown variant from content({ presets }). Every file the collection reads compiles with that preset’s settings merged over the app’s markdown settings: other themes, other transformers, overrides on or off. The app’s other markdown is unchanged.

Each file compiles as its own variant, so another collection that reads the same file with another preset, or none, renders it independently. The name has to be a string in the source; anything else throws content_preset_not_literal, and an options object the build can’t read throws content_loader_options_unparsable. A name that isn’t configured throws content_preset_unknown, listing the ones that are.

import { content } from '@ogygia/content';

export const blog = content({
  loader: import.meta.og.loader.folder('../content/blog', {
    preset: 'plain'
  })
});

Rules

  • The path is a string in the source. A variable fails with call_arg_not_literal, a template literal with ${…} with call_arg_interpolated, and an expression around the string ('./a' + b) with call_arg_in_expression. A source that changes at runtime, a CMS or a database, is a source you write.
  • Only the four names exist. import.meta.og.loader.yaml() fails with content_loader_unknown.
  • Loaders go in script modules. In a .svelte component the call isn’t rewritten and fails when it runs; the build warns with content_loader_in_component. Put the collection in a server-only .ts module, as Collections explains.
  • The source builders are there for the rest. markdown(), folder() and json() from @ogygia/content are what the macros become. Call them yourself when you need a glob map the macro can’t write.