Docs as code
Docs as code means documentation is written, reviewed, tested and released the way code is: as plain text in the repository, changed in pull requests, checked by CI and published by a build. A change to the code and to its documentation is then one change.
This page sets it up with svelte-edda, for a library whose code and docs share a repository.
-
Keep the docs beside the code.
my-library/ docs/ index.md guide/install.md src/ package.json svelte-edda.config.tsThe pages are ordinary Markdown, so a reviewer reads them on GitHub without building anything. See getting started.
-
Make docs part of the change. A pull request that changes behaviour changes the page that describes it. Say so in
CONTRIBUTING.md, and show agents the syntax:The documentation in `docs/` is built with svelte-edda. Before editing it, run `npx svelte-edda guide` and follow what it prints. After editing, run `npx svelte-edda check` and fix everything it reports. -
Test the docs in CI.
svelte-edda checkfails on a broken link or anchor, a misspelled component, an attribute that does not exist, a live demo that does not compile. Put it beside the other checks, in.github/workflows/ci.yml:name: CI on: [pull_request] jobs: docs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 with: # archived versions come from git tags fetch-depth: 0 - uses: oven-sh/setup-bun@v2 - run: bun install --frozen-lockfile - run: bunx svelte-edda check -
Generate what can be generated. A reference copied by hand goes stale. Write it with a tool and commit the result: with
commit: true,checkcompares the committed pages with fresh output, and an outdated reference fails the pull request.import { defineConfig, svelteComponents } from 'svelte-edda'; export default defineConfig({ title: 'My library', generate: [ svelteComponents({ dir: 'components', components: ['src/lib/*.svelte'], commit: true }) ] });See generated pages for TypeDoc and other tools.
-
Publish on merge. A push to
mainbuilds the site and deploys it, see deployment. -
Archive a release with its tag. The docs of an older version are the docs directory at that tag, so there is nothing to copy:
export default defineConfig({ title: 'My library', versions: [{ tag: 'v1.9.0', label: '1.x' }] });See versions.
What to check for in review
- The page says what changed for the reader, not how the code changed.
- Every new option is there with its default, and every new error with what to do about it.
- Code samples run. A
svelte livecode block is compiled bycheck; other samples are not, so try them. - The page has a
description: search engines andllms.txtshow it.
Where it does not fit
Docs as code asks every author to use git. When most of the writing is done by people who do not, such as support or marketing, a hosted platform with an editor serves them better: see GitBook and Mintlify.