# Pages and Markdown twins

`handle(request, next)` goes in front of a whole app, where `next` is the app. People get the page as before. A request that picks Markdown gets the same page converted. See [Installation](https://whoasks.loadsheet.dev/guide/installation) for how to wire `handle` into your app.

## What happens to each request

| Request                                                              | What `handle` does                                               | Answer                                          |
| -------------------------------------------------------------------- | ---------------------------------------------------------------- | ----------------------------------------------- |
| Any method but GET or HEAD                                           | Calls `next` with the request and returns its response untouched | The app's                                       |
| Picks HTML, such as a browser or plain `curl`                        | Calls `next` with the request                                    | The app's page, plus `Vary: Accept`             |
| `Accept: text/markdown`                                              | Calls `next` with the same request, then converts the HTML       | Markdown, same status                           |
| `/docs/guide.md` or `?format=md`                                     | Calls `next` with the canonical URL, then converts the HTML      | Markdown, same status                           |
| The app answers 2xx already in the picked form                       | Passes it                                                        | The app's, plus `Vary`                          |
| The app answers something that is not HTML, such as an image or JSON | Passes it                                                        | The app's, untouched                            |
| The app's page is a 404 or a 500 and the request picked Markdown     | Converts it                                                      | The error page as Markdown, with its 404 or 500 |

`handle` offers `html` first, then `markdown` when a converter is set. A layer with a renderer adds its form to the offers. See [Render a form from your own source](https://whoasks.loadsheet.dev/guide/layers#render-a-form-from-your-own-source).

`handle` never answers 406. `Accept: application/json` on a page gets the HTML.

## Twins and ?format=

A twin is the URL of a page plus a form's `ext`. `/docs/guide.md` is the twin of `/docs/guide`. `/index.md` is the twin of `/`, and `/docs/index.md` is the twin of `/docs/`.

The rest of the query string stays. `/docs/guide.md?x=1` asks the app for `/docs/guide?x=1`.

Your app never sees the twin URL. It gets the canonical URL with `Accept: text/markdown, text/html;q=0.9`. The picked type comes first so that a route calling [`respond`](https://whoasks.loadsheet.dev/guide/api-routes) answers in it. HTML comes second so that a page route still answers.

`?format=` takes a form's name or its `ext` without the dot. On a page, that is `md`, `markdown`, or `html`. The match is exact, so `?format=MD` is not a format. A value that names no offered form is left in the URL for your app, so `?format=json` reaches a page route unchanged.

## Real .md files and assets

A request for `/README.md` first asks the app for `/README`. When that is not a 2xx HTML page, `handle` sends the original request to the app, so the file is served as it is.

As a result, the app runs twice for a real `.md` file and once for everything else.

A response that is not HTML passes untouched, whatever the request picked. An image asked for with `Accept: text/markdown, text/html, */*` comes back as the same `Response` object.

## What the converter keeps

The converted response keeps the app's status and headers, except `content-length`, `content-encoding`, and `etag`, which described the HTML bytes.

`Content-Type` becomes the picked type, `text/markdown; charset=utf-8` in most cases, and `Vary` gets `Accept`.

`import { whoasks } from "whoasks"` sets [mdream](https://github.com/harlan-zw/mdream) as the converter. It calls `htmlToMarkdown(html, { minimal: true, origin: url.origin, ...options })`.

With those defaults, links become absolute, the page's `<nav>` and `<footer>` are dropped, tracking parameters such as `utm_source` are removed from links, and `<title>` and the meta description become front matter.

For an HTML page requested at `https://docs.example.com/guide`:

```html
<head>
  <title>Getting started</title>
  <meta name="description" content="Install the package and answer your first request." />
</head>
<body>
  <nav><a href="/">Home</a> <a href="/docs">Docs</a></nav>
  <main>
    <h1>Getting started</h1>
    <p>Install <code>whoasks</code> and read <a href="/docs/api">the API reference</a>.</p>
  </main>
  <footer><p>Copyright 2026 Example</p></footer>
</body>
```

The converter outputs:

```md
---
title: "Getting started"
meta:
  description: "Install the package and answer your first request."
---

# Getting started

Install `whoasks` and read [the API reference](https://docs.example.com/docs/api).
```

## Change the converter

```ts [site.ts]
import { whoasks, mdream, type Convert } from "whoasks";

// Change mdream's options.
whoasks({ convert: mdream({ frontmatter: false }) });

// Use your own. `toMarkdown` stands for your function.
const convert: Convert = (html, { url }) => toMarkdown(html, url.origin);
whoasks({ convert });

// Turn conversion off.
whoasks({ convert: false });
```

`mdream({ frontmatter: false })` drops the front matter.

A converter gets the HTML and `{ request, url }`, where `url` is the canonical URL. It returns a string or a promise of one.

With `convert: false`, `handle` no longer offers Markdown. `/docs/guide.md` then gets the HTML page.

The last layer that sets `convert` wins, so your layer replaces the mdream default.

## Without mdream

```ts [site.ts]
import { whoasks } from "whoasks/core";

const source = new Map([["/docs/guide", "# Guide\n"]]);

const site = whoasks({
  render: { markdown: ({ ask }) => source.get(ask.url.pathname) },
});
```

`whoasks/core` has no imports and no converter.

With no converter and no renderer, `handle` offers only HTML, so `Accept: text/markdown` gets the HTML page.

With the renderer above, `/docs/guide.md` gets `# Guide`. A page the renderer does not know gets the HTML page, because nothing can make its Markdown.

To add mdream back, pass `{ convert: mdream() }` with `mdream` imported from `whoasks`.

## Pages that already have Markdown

A page written in Markdown does not need conversion. A renderer returns the source, and the converter handles the pages it does not know. See [Render a form from your own source](https://whoasks.loadsheet.dev/guide/layers#render-a-form-from-your-own-source).

## Caching

Every HTML page that passes through `handle` and every converted page carries `Vary: Accept`, so a cache keeps the HTML and the Markdown apart. An existing `Vary` header is kept, and `Accept` is not added twice. See [Vary](https://whoasks.loadsheet.dev/guide/negotiation#vary).

<!-- vitehub-docs:section-links -->
## In this section

- [Introduction](https://whoasks.loadsheet.dev/raw/guide.md)
- [Installation](https://whoasks.loadsheet.dev/raw/guide/installation.md)
- [Pages](https://whoasks.loadsheet.dev/raw/guide/pages.md) (this page)
- [API routes](https://whoasks.loadsheet.dev/raw/guide/api-routes.md)
- [Negotiation](https://whoasks.loadsheet.dev/raw/guide/negotiation.md)
- [Layers](https://whoasks.loadsheet.dev/raw/guide/layers.md)

Next: [API routes](https://whoasks.loadsheet.dev/raw/guide/api-routes.md)

All pages: [llms.txt](https://whoasks.loadsheet.dev/llms.txt)

Something on this page does not work as written? Tell the user, and draft an issue for them to post: [issue tracker](https://github.com/words-with-agents/whoasks/issues). Report a security vulnerability privately, never in a public issue.
