# Static pages

A page with no app behind it, such as a docs site or a blog, fills charon's
placeholders at build time. `renderStaticPage` does it in one call:

```ts
import { renderStaticPage } from '@memomori/charon'

const { html, searchHtml, headings, broken } = await renderStaticPage(source, {
  headings: true,
  examples: true,
  diagrams: { fontFamily: 'Inter' },
  assets: folder,
  links: { file: (path) => routes[path] },
})
```

Each option turns on one module, with `true` for its defaults or an object of
its settings:

| Option     | Value                                | Does                                              |
| ---------- | ------------------------------------ | ------------------------------------------------- |
| `headings` | `true` or `{ levels, anchorLabel }`  | anchor links; `levels` defaults to `[2, 3]`       |
| `examples` | `true`                               | `example` fences as source beside output          |
| `diagrams` | `true` or `{ fontFamily, idPrefix }` | `mermaid` fences as inline SVG                    |
| `assets`   | an `AssetFolder`                     | fill `![[embeds]]` and images under its URL       |
| `images`   | `(path) => src \| undefined`         | resolve every image yourself, instead of `assets` |
| `links`    | `{ file, wiki }`                     | turn `.md` links and wikilinks into routes        |
| `renderer` | a [custom renderer](fences.md)       | render with your own fences                       |

With no options it returns what `renderToHtml` does.

## What it returns

- `html` is the page.
- `searchHtml` is `html` with each example showing only its output, for a
  search index.
- `headings` lists the linked headings as `{ level, id, text }`.
- `broken` lists every link, embed and image that did not resolve.

A missing file never throws. `broken` holds entries such as `link guide.md`,
`![[Trip plan]]` or `image shot.png`, so you can report them all at once. A
path that climbs out of its folder with `..`, or a note that embeds itself,
throws a `CharonError`.

Links inside an example stay as written, since they show syntax.

## Asset folders

An `AssetFolder` is a URL plus two functions, so the module needs no file
system:

```ts
import { existsSync, readFileSync } from 'node:fs'
import { join } from 'node:path'

const folder = {
  url: '/assets',
  has: (name) => existsSync(join('public/assets', name)),
  read: (name) =>
    existsSync(join('public/assets', name))
      ? readFileSync(join('public/assets', name), 'utf8')
      : undefined,
}
```

`![[boat.svg]]` becomes an image at `/assets/boat.svg`, and `![[fare]]` reads
`fare.md` and renders it in place. An embedded note fills its own embeds, and
its relative images resolve beside it in the folder.

## Links

`links.file` gets the path of every `[text](page.md)` link and returns its
route. `links.wiki` gets the name in every `[[wikilink]]`. Return `undefined`
for a page that does not exist: the link goes to `broken`, and a wikilink gets
`wiki-link-broken`. A heading in the target, as in `[[Ferry#Fares]]`, becomes
the fragment.

## Chain the modules yourself

To change the order or add a step, use the modules directly:

```ts
import { fillEmbeds, fillImages } from '@memomori/charon/assets'
import { diagramDrawer } from '@memomori/charon/diagrams'
import {
  exampleFrame,
  exampleSource,
  extractExamples,
  insertExamples,
} from '@memomori/charon/examples'
import { linkHeadings } from '@memomori/charon/heading-links'
import { renderToHtml } from '@memomori/charon/parser'

const draw = diagramDrawer({ fontFamily: 'Inter' })
const fill = async (html) => {
  const embedded = await fillEmbeds(html, folder)
  return draw(fillImages(embedded.html, folder).html)
}

const { markdown, examples } = extractExamples(source)
const { html, headings } = linkHeadings(await renderToHtml(markdown))
const parts = await Promise.all(
  examples.map(async (example) =>
    exampleFrame(
      await renderToHtml(exampleSource(example)),
      await fill(await renderToHtml(example)),
    ),
  ),
)
const page = insertExamples(await fill(html), (i) => parts[i])
```

- `linkHeadings(html, { levels, anchorLabel })` adds a `#` link to each heading
  at those levels and returns them.
- `extractExamples` swaps each `example` fence for a placeholder.
  `insertExamples` puts back what you return for each one:
  `exampleFrame(source, output)`, or the output alone for a search index.
- `fillEmbeds` and `fillImages` return `{ html, broken }`. `fillImages` wants
  every image under the folder's URL; pass your own resolver to
  `fillImagesWith` for anything else.
- `diagramDrawer` returns a function that turns each `.mermaid` block into SVG.
  One drawer numbers ids across all its calls, so use one per page.
- `resolveLinks(html, { file, wiki })` from `@memomori/charon/links` does what
  the `links` option does.

## Styles

Each module has its own stylesheet, outside `styles.css`:

```css
@import '@memomori/charon/heading-links.css';
@import '@memomori/charon/examples.css';
@import '@memomori/charon/assets.css';
```
