GitHub 0
Skip to content

Introduction to whoasks

whoasks reads each request's Accept header, .md twin URL or ?format= and answers the same URL as HTML, Markdown or JSON.

A page is written for people. An agent that fetches it gets markup to strip, and a program wants data. whoasks answers each request in the form its reader can use, at the same URL.

A form is a shape an answer can take. Each form has a name, a list of media types, and sometimes an ext suffix.

FormForMedia typesext
htmlPeople, browserstext/htmlnone
markdownAgentstext/markdown, text/x-markdown, text/plain.md
jsonProgramsapplication/jsonnone

A layer can add more forms. See Add a form.

whoasks checks three things in order to make its pick:

  1. A twin URL. /docs/guide.md asks for the Markdown of /docs/guide.
  2. ?format=. /docs/guide?format=md asks for the same form. It is for a client that cannot set headers.
  3. The Accept header. Accept: text/markdown asks for Markdown.

When the request states no preference, the answer is the first form the route offers. For a page, that default offer is HTML. See Negotiation for all seven rules.

whoasks() returns a site with three methods (ask, respond, and handle) plus forms.

PieceWhat it does
whoasks(...layers)Merges the layers and returns a site
site.handle(request, next)Goes in front of a whole app. Converts its HTML pages to Markdown when a request picks Markdown
site.respond(request, renders)Answers one route, from one value per form
site.ask(request)Returns the pick alone, without building a response
whoasks/h3middleware(site), which plugs handle into an h3 app
whoasks/coreThe same whoasks with no converter and no imports
mdream(options)The default converter, with your options
You haveUsePage
A site of HTML pageshandle, once, in front of the appPages
A route that returns datarespond in that routeAPI routes
BothBoth. A route that calls respond works behind handleAPI routes
Your own response code that only needs the decisionaskAPI reference
A content source or a framework to plug inA layerLayers

These choices are deliberate:

  • It does not read the User-Agent. Two clients with the same Accept get the same answer, unless a layer says otherwise.
  • handle never answers 406. A request that allows nothing offered gets the page as HTML.
  • The only adapter in the package is h3. Any other runtime calls handle directly.
  • No Nuxt module, no llms.txt generator, and no agent text format. Not included shows how to build each.

Read Installation, then Pages for a site or API routes for a data route.