@ogygia/content · Docs sites
Link checking
Checks run over your pages and report what's wrong before a reader finds it. The built-in links() check fails the build on a link to a page or heading that doesn't exist, and you can add rules of your own.
On this page
A link to a page that moved, or to a heading someone renamed, is the kind of mistake nobody notices until a reader clicks it. A check looks at your pages and reports problems as findings. A finding marked as an error stops the page: the build fails while you prerender, and the page fails to render while you write in dev. links() is the check that ships, and a check is a small object, so you can write your own.
Turn links() on from the first page. Every link to an address that no longer exists fails the build with the file and line. If you add redirect_from when you move a page, the old links keep working and the check warns until you update them; redirected: 'error' makes those fail too. This site runs it, so every link on this page was checked when it was built.
import { content, links, site } from '@ogygia/content';
const guides = content({
loader: import.meta.og.loader.folder('../content/docs')
});
export const docs = site({
outline: guides,
checks: [links()]
});Link check
links() reads each page’s markdown links, which the markdown format collects into meta.links along with their text and line, and tests each one against the site’s own addresses:
/docs/guides/routing, a path under the site’s mount prefix: the page must exist. When it exists, a#fragmentmust be the id of one of its headings.#setup, a fragment on its own: the page itself must have that heading.- An old address from some page’s
redirect_fromstill works, through a308, but is out of date. That’s a warning by default, which names the address to use instead.
It leaves alone what it can’t judge: links with a scheme (https:, mailto:), //host links, paths outside the mount prefix (/blog/… on a site mounted at /docs), and relative links such as ./diagram.png or routing, which include the bare addresses Link resolves. Those bare addresses go unchecked today (#2284).
On a site mounted at the root, every path starting with / is a site address to the check, so a link to another route of your app (/blog, /about) is a missing page. Skip those with ignore: (href) => href === '/blog' || href.startsWith('/blog/'). A bare startsWith('/blog') would also skip your own pages, such as /blogging.
It knows every address of the site, so it understands old addresses and, on a site with dimensions, every version and language.
A collection whose entries have no meta.links (JSON, a CMS, blocks) can’t be link-checked. In dev the check says so once per collection, with content_links_no_data, and lets those pages pass. Any source can opt in by filling meta.links with { href, text, line? } items.
Tune the link check
| Option | Default | What it does |
|---|---|---|
anchors | true | false stops checking #fragments against headings. |
redirected | 'warn' | A link through an old address: 'error' fails, 'warn' reports, 'ok' says nothing. |
ignore | none | (href) => boolean; true skips the link, for paths the site doesn’t own: generated files, or other routes of your app on a root-mounted site. |
import { content, links, site } from '@ogygia/content';
const guides = content({
loader: import.meta.og.loader.folder('../content/docs')
});
export const docs = site({
outline: guides,
checks: [
links({
redirected: 'error',
ignore: (href) => href.startsWith('/docs/api/')
})
]
});Reporting
Checks run in two places.
In docs.load, for one page. Every time a page’s route runs docs.load, the site runs each check on that page. Error findings throw content_check_failed, which names the page and its source file and lists every finding: the check, the link, what’s wrong (missing page, missing anchor #setp), the link’s text and its line. Warnings are logged as content_check_warned and the page renders. So a broken link fails the prerender of its page, and shows as an error when you open the page in dev. This also happens on a server that renders pages per request: a page with an error finding fails for the reader too, so catch it before deploying with docs.check().
In docs.check(), for every page. It runs every check over the whole site and returns the findings as data, without throwing. Use it in a test or a CI step, which matters most on a site that isn’t prerendered, where no build visits every page. Pass the mount prefix as base (or set it on the site), because links are judged against it: without it, every /docs/… link looks like a missing page.
A finding is plain data: check (the check’s name), severity ('error' or 'warn'), message, and when known slug, file (the page’s source file) and line (approximate).
import { expect, test } from 'vitest';
import { docs } from '$lib/docs.server';
test('every link in the docs resolves', async () => {
const findings = await docs.check({ base: '/docs' });
expect(
findings.filter((f) => f.severity === 'error')
).toEqual([]);
});Custom checks
A check is an object with a name and one or both of two functions, each returning findings (or a promise of them):
page(slug, cx)checks one page. It runs inloadfor each page, anddocs.check()runs it over every page when the check has nosite.site(cx)checks the whole site at once, fordocs.check()only. Use it for rules about the whole (no two pages with one title, say).
cx holds outline (to look up addresses), base and ctx, the read context to pass on. On a site with dimensions, outline.tree takes the coordinate before the context, so read the tree the way Read the outline yourself shows (#2286). Here a house rule requires every page to have a summary:
import type { Check, Finding } from '@ogygia/content';
export function requireSummary(): Check {
return {
name: 'require-summary',
async page(slug, { outline, ctx }): Promise<Finding[]> {
const hit = await outline.resolve(slug, ctx);
if (!hit) return [];
const entry = await hit.collection.get(
hit.record.entryId,
ctx
);
if (!entry || entry.data.summary) return [];
return [
{
check: 'require-summary',
severity: 'error',
slug,
...(hit.record.filePath
? { file: hit.record.filePath }
: {}),
message: `'${slug}' has no summary; llms.txt and the keep-reading cards show it.`
}
];
}
};
}import { content, links, site } from '@ogygia/content';
import { requireSummary } from './checks';
const guides = content({
loader: import.meta.og.loader.folder('../content/docs')
});
export const docs = site({
outline: guides,
checks: [links(), requireSummary()]
});Checks vs outline errors
Checks are rules you choose: you turn them on, set their severity, and can allow a dead link for a while during a migration. Some problems aren’t a matter of choice, because a site that has them isn’t well formed: a page placed nowhere, two pages at one address, a pick() that matches nothing. Those are outline errors. They’re always on, and they fail every read of the site, not one page.
Do and don’t
- Do turn on
links()from the start. It catches a broken link at build time, before a reader does. - Do run
docs.check()in CI for a site that renders per request, where no prerender visits every page. - Do pass
basetodocs.check(), or set it on the site. - Don’t silence a finding with
ignorewhen the fix is to change the link or the heading.