doc @diso.io/doc
Guide
Docs sites for libraries. A site is a list of pages, drawn as a hype app and routed by groutcho. A Markdown file is one kind of page. The build also writes every page as an HTML file with its content in it, and as Markdown with an index, so the site is read by people, by crawlers and by programs.
npm install @diso.io/doc @diso.io/hype @diso.io/groutcho
A site
A site is a title and its pages, in the order they are listed.
// src/site.ts
import type { Site } from "@diso.io/doc";
import guide from "../docs/guide.md";
import readme from "../README.md";
import playground from "./playground";
const site: Site = {
title: "My library",
description: "What the library is for, in a sentence or two.",
links: [{ label: "GitHub", href: "https://github.com/me/my-library" }],
pages: [
{ path: "/", ...readme },
{
title: "Learn",
pages: [
{ path: "/guide", ...guide },
{ path: "/playground", ...playground },
],
},
],
};
export default site;
pages holds pages and groups of pages. A group is a title and its own
pages, and can hold groups, as deep as the site needs. The navigation lists
them in the order written and never sorts, so put first what most readers
need. A group at the top is a heading. A group inside a group is closed until
the page being read is in it or the reader opens it, which goes to its first
page; once open it stays open until the reader closes it. A group's link, a label and an
href, is shown small beside its title: the name of the package the group
documents, linked to where it is published.
Because the navigation shows where a page is, titles can be short and repeat: every part of a site can have its own "Guide" and "Reference". Where a page's place does not show, in the browser tab and in what search finds, a page whose title another page shares is named with its groups.
A page is headed with its title, exactly as the navigation has it, and the
groups it is in are written small above it: "groutcho › Other frameworks ›
React" over "Guide". Beside them is the link of the nearest group that has
one, so the page names the package it documents. A Markdown page's own top heading is replaced by the
title; a page that is not Markdown is given the title as draw's argument.
path |
Where the page is served, such as / or /guide/install. |
title |
The page's name in the navigation and the browser tab. |
draw |
A function returning what the page shows: anything a hype template takes. |
headings |
The headings on the page, listed beside it. |
markdown |
The page as Markdown, served beside it for programs. |
load |
A function that fetches draw, headings and markdown the first time the page is opened, in place of giving them. |
wide |
Give the page the full width, with no navigation or headings beside it. |
unlisted |
Leave the page out of the navigation, for one the header links to. |
A Markdown file imported as a module gives title and load, so it is spread
into a page as above. A visitor downloads a Markdown page's content when they
open it, and never the content of pages they do not open.
A folder of pages
pages lists a folder's files in the order and groups given, so a site with
many pages names each once.
import { pages, type Site } from "@diso.io/doc";
const site: Site = {
title: "My library",
pages: pages(import.meta.glob("../docs/**/*.md", { eager: true, import: "default" }), [
"index",
{ title: "Guide", pages: ["guide/install", "guide/theming"] },
]),
};
A page is named by its file's path inside the folder, without the extension,
and is served there: guide/install.md is at /guide/install. An index is
served at its folder, the one at the top at /. Keep the pages of a group in
a folder of their own, so the files are grouped as the navigation is. A name
with no file, and a file that is not named, are both errors.
A page that is not Markdown
Any page can be written as a module. draw returns a hype template, so the
page can hold elements, state and anything else the browser runs.
// src/playground.ts
import { html, signal } from "@diso.io/hype";
const count = signal(0);
export default {
title: "Playground",
draw: () => html`
<h1>Playground</h1>
<button @click=${() => count.value++}>clicked ${() => count.value} times</button>
`,
};
A page that is large, or that few visitors open, can be fetched when it is
opened. Give load in place of draw, and have the module export what the
page shows:
{
path: "/playground",
title: "Playground",
load: () => import("./playground").then((made) => made.default),
}
The page already on screen stays until the new one has arrived.
A reference made from the code
A package's reference is a page made from its code, beside the pages written by hand. It is made again on every build.
It follows how the code is laid out, so the layout is what organises it:
src/
index.ts the entry file: it only exports, from the folders below
signals/ a folder is one piece of what the package does
index.ts what the piece makes public
elements/
diagrams/ a folder of folders is a group of pieces
class/
flowchart/
| In the code | In the reference |
|---|---|
An entry file: what package.json exports |
A page |
| The comment at the top of the entry file | How the page opens |
A folder in src/, in the order the entry file exports from them |
A section, titled with the folder's name |
The comment at the top of the folder's index.ts |
How the section opens |
| A folder inside a folder | A section inside the section |
| Something the entry file exports | A heading: how it is declared, its comment, and each member of an interface or class |
| Something the entry file does not export | Nothing |
A section opens with a table of what is in it, each with the first sentence of its comment, so write that sentence to stand alone. A type sits beside the functions that use it, because they are in the same folder. A file beside the entry file is a section of its own, titled with the file's name. What a package only passes on from another package is named at the end, and described in that package's reference.
A package with more than one entry file, such as my-library and
my-library/extend, has a page for each: an entry file is for one kind of
use, and its page says which.
For a TypeScript package, import an entry file with ?reference:
import reference from "../src/index.ts?reference";
const site: Site = {
title: "My library",
pages: [
{ path: "/", ...readme },
{ path: "/reference", ...reference, title: "Reference" },
],
};
On a site the page is headed with its title, as every page is.
The same page as Markdown text comes from reference, for writing to a file
or anything else:
import { reference } from "@diso.io/doc/reference";
writeFileSync("docs/reference.md", reference("src/index.ts"));
For a language other than TypeScript, write the reference as a Markdown file with that language's own tools and list it as a page.
The page that holds it
docs(site) defines the <diso-docs> element as the site: a header, the
navigation, the page for the current address, and the headings on that page.
// src/main.ts
import "@diso.io/doc/style.css";
import { docs } from "@diso.io/doc";
import site from "./site";
docs(site);
<!-- index.html -->
<body>
<diso-docs></diso-docs>
<script type="module" src="/src/main.ts"></script>
</body>
A link to another page of the site, written anywhere inside it, goes there
without loading the page again. An address with no page shows a "Not found"
page; give the site its own by adding a page at /404.
Under each page are links to the page before it and the page after it, in the order the navigation lists them, so the site reads from start to end.
The header has a search box. It finds pages and the headings inside them by
the words typed, and fetches its index the first time it is used. / puts the
cursor in it, Enter opens the first result and Escape clears it.
The look is @diso.io/doc/style.css, in light and dark. The header has a
choice of three: follow the device, light, or dark. It starts following the
device, and a choice is kept for their next visit. Every site has it, so the sites of one family look like one
family. A site changes it by setting --docs-* values on :root: its accent
colour, its fonts, its widths. Text is --docs-width wide; a site with
something wider to show, such as a drawing beside its source, gives the room
for it as --docs-wide. What a site adds of its own is coloured with
the same values, so it follows light and dark too. The site is drawn directly in
the page, with no shadow root, so the page's own CSS applies to everything.
The build
The Vite plugin reads Markdown files as pages and writes the site's files.
// vite.config.ts
import docs from "@diso.io/doc/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [docs({ site: "src/site.ts" })],
});
For TypeScript to know what a Markdown import and a stylesheet import are, add the types:
{ "compilerOptions": { "types": ["vite/client", "@diso.io/doc/markdown"] } }
| Option | |
|---|---|
site |
The module whose default export is the site, from the project root. |
markdown |
A function given the markdown-it reader, to change how Markdown is read: drawing a kind of fenced block, for example. |
link |
A function given a link as written and the file it is in, returning where it goes on the site. |
In a Markdown page:
- The first top-level heading is the page's title, until the site gives it another.
- Each heading gets the
idGitHub would give it, so a#fragmentlink that works on GitHub works on the site. - A link to another Markdown file goes to that page:
guide.mdbecomesguide, andindex.mdorREADME.mdbecomes its folder. This is right when pages are served where their files are. When they are not, givelink. - An image beside the file is served by the build.
- A fenced block of
ts,tsx,js,jsx,html,sh,bashorjsonhas its comments, strings, keywords and numbers coloured, and in code that is run, the names of functions and the types. It is done when the site is built, so the page downloads nothing for it. The colours are the--docs-code-*values.
A fenced block can become a live element: have markdown write a custom
element for it, and define that element with hype. It is drawn when the site is
built and comes alive in the page. readMarkdown and slug, from
@diso.io/doc/vite, are there for a site's own tests.
What the build writes
For a site with pages at / and /guide:
dist/
index.html the page at /, with its content in it
guide.html the page at /guide
404.html the page for unknown addresses
index.md the page at / as Markdown
guide.md
llms.txt the site's title, description and pages, linked as Markdown
search.json the text of every page, by heading, for the search box
- For people and crawlers, each HTML file holds the page's whole content, so it reads before any script runs and without one. When the script loads, the site draws over it and is an app from then on.
- For programs, each page that has Markdown is served as Markdown at its
path with
.mdon the end, and/llms.txtlists them. Each HTML file links to both.
A host that serves guide.html at /guide needs nothing more. Cloudflare
Workers static assets and GitHub Pages both do.
What it does not do yet
- Docs for more than one version of a library.