GitHub 0
Skip to content

Pages and Markdown twins

handle wraps a whole app. HTML pages become Markdown for agents, and real .md files, assets and JSON routes pass through as they are.

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 for how to wire handle into your app.

RequestWhat handle doesAnswer
Any method but GET or HEADCalls next with the request and returns its response untouchedThe app's
Picks HTML, such as a browser or plain curlCalls next with the requestThe app's page, plus Vary: Accept
Accept: text/markdownCalls next with the same request, then converts the HTMLMarkdown, same status
/docs/guide.md or ?format=mdCalls next with the canonical URL, then converts the HTMLMarkdown, same status
The app answers 2xx already in the picked formPasses itThe app's, plus Vary
The app answers something that is not HTML, such as an image or JSONPasses itThe app's, untouched
The app's page is a 404 or a 500 and the request picked MarkdownConverts itThe 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.

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

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 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.

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.

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 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:

<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>
html

The converter outputs:

---
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).
md
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 });
site.ts

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.

import { whoasks } from "whoasks/core";

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

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

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.

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.

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.