Skip to content

@ogygia/content · Docs sites

Sitemap and RSS

The site writes the files crawlers, language models and feed readers ask for, each except the RSS feed from the same outline as the sidebar, so none of them can drift from the site. Each one is a GET handler you mount in a +server.ts with one line.

On this page

Some readers of a docs site aren’t people. Search engines want sitemap.xml, language models read llms.txt, feed readers poll an RSS feed, and a “copy as markdown” button wants each page’s source. docs.emit makes each of these, except the RSS feed, from the outline, the same tree the sidebar shows, so a new page appears in all of them as soon as it appears in the sidebar.

Each emission is a function that returns a Kit GET handler. You mount it at the path you want in a +server.ts and usually prerender it:

EmissionAnswers withTypical path
emit.sitemap(options?)sitemap.xml, every page as an absolute URL/sitemap.xml
emit.llms(options?)llms.txt, the site as a markdown index/llms.txt
emit.raw(options?)each page’s markdown source/docs/[...slug].md
emit.rss(options)an RSS 2.0 feed/blog/rss.xml
emit.search()the search index/docs/search.json; see Search

All of them except RSS read the public view: drafts and pages that only a read context shows are left out. The RSS feed lists exactly what its items function returns.

import { docs } from '$lib/docs.server';

export const prerender = true;
export const GET = docs.emit.sitemap({ base: '/docs' });

Origin

A sitemap and a feed need absolute URLs, and a prerendered file has no real request to read the origin from. Each emission takes its origin from, in order: its own origin option, the site’s data.origin, and finally the request’s origin. While prerendering, that last one is Kit’s kit.prerender.origin, which defaults to http://sveltekit-prerender. Set the origin once on the site, and every emission is right:

base works the same way: each emission’s base option, else the site’s base, else ''. With both set on the site, as above, docs.emit.sitemap() needs no options.

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

const guides = content({
  loader: import.meta.og.loader.folder('../content/docs')
});

export const docs = site({
  outline: guides,
  base: '/docs',
  data: {
    title: 'Example',
    description: 'Docs for Example.',
    origin: 'https://example.com'
  }
});

Sitemap

emit.sitemap({ base?, origin? }) lists every page in the sidebar tree, in reading order, as <url><loc>…</loc></url> with the origin and base in front. Plain links in the outline aren’t pages and aren’t listed. It answers with content-type: application/xml.

llms.txt

emit.llms({ base?, origin?, title?, description? }) writes an llms.txt index: a # heading with the title, the description as a quote, then the pages as a list of links with their summaries. Pages and links at the top of the tree go under ## Pages; each top-level group gets its own ## heading, with the pages of groups inside it listed under it. title defaults to the site’s data.title, then to Documentation; description to data.description. It answers as text/plain.

Here’s the start of the file, with one line left out of the sample: in the real file, each group’s links follow under that group’s ## heading (## Getting started), which sits between the quote and the links:

import { docs } from '$lib/docs.server';

export const prerender = true;
export const GET = docs.emit.llms({
  base: '/docs',
  title: 'Example',
  description: 'Docs for Example.'
});
# Example

> Docs for Example.

- [Overview](https://example.com/docs/start/overview): What Example is and when to use it.
- [Install](https://example.com/docs/start/install): Add Example to an app.

Raw markdown

emit.raw({ frontmatter? }) serves each page’s source file, for a “copy as markdown” button or a model that wants the original text. Unlike the others it returns two things, GET and entries, and it mounts on a rest route ending in .md whose parameter is named slug:

/docs/routing.md then serves the source of /docs/routing. This site does the same: add .md to the address of this page.

  • The frontmatter block is taken off by default, since it’s site settings rather than prose. frontmatter: 'keep' keeps it.
  • Only pages with a source are served. A page from JSON or a CMS has none: entries leaves it out, so the prerenderer never asks for it, and GET answers 404 for it and for an unknown address.
  • On a site with dimensions, every version and language is served, at its own prefixed address.
  • It answers as text/markdown; charset=utf-8.
import { docs } from '$lib/docs.server';

export const prerender = true;
export const { GET, entries } = docs.emit.raw();

RSS

emit.rss({ title, items, description?, base?, origin? }) writes an RSS 2.0 feed. Unlike the others it doesn’t read the outline: items is a function that returns the posts (or a promise of them), each { href, title, date, description? }. href is a path from the root, which the origin is put in front of. date is YYYY-MM-DD, written as the item’s pubDate at midnight UTC. Items are sorted newest first whatever order you give them. The channel’s link is the origin plus base.

A blog keeps its dates in frontmatter (fields.post requires date), so map the collection’s entries:

import { blog, posts } from '$lib/blog.server';

export const prerender = true;

export const GET = blog.emit.rss({
  title: 'Example blog',
  base: '/blog',
  items: async () =>
    (await posts.refs()).map((p) => ({
      href: `/blog/${p.id}`,
      title: p.data.title,
      description: p.data.summary,
      date: p.data.date
    }))
});

Limits

  • On a site with dimensions, the sitemap and llms.txt list only the default version and language.
  • Each emission is built when it’s requested. Prerender them, or they rebuild on every request.
  • emit.search() takes no options; the search box applies base when it shows hits.

Do and don’t

  • Do set origin once in the site’s data rather than on each emission.
  • Do prerender every emission on a static site.
  • Don’t write these files by hand. Generated from the outline, the sitemap, llms.txt and the raw pages can’t list a page the site doesn’t have or miss one it does.