# Markdown answers

Some commands answer with a page, not findings: an `explain` command, a help topic, a report. readerly doesn't render Markdown and has no dependencies. You render the page yourself for the reader who wants it styled, and hand readerly the string with `out.write()`.

## The recipe

[@comark/ansi](https://www.npmjs.com/package/@comark/ansi) turns Markdown into terminal text. Render only when the format is `text`, and pass the Markdown as is to everyone else:

```ts [explain.ts]
import { renderAnsi } from "@comark/ansi";
import { createOut, readerFlags } from "readerly";

export async function explain(argv: string[]): Promise<number> {
  const out = createOut({ tool: "acme", command: "explain", argv });
  const [code] = readerFlags(argv).rest;
  const page = await pageFor(code); // Markdown
  out.write(
    out.profile.format === "text" ? await renderAnsi(page, { colors: out.profile.color }) : page,
  );
  out.next("acme check", "finds every ACME001");
  return out.end();
}
```

`pageFor` stands for your own lookup. `readerFlags(argv).rest` drops the reader flags, so `acme explain --json ACME001` still finds the code.

`renderAnsi` is async, so it runs before `out.write`, not inside a [renderer](https://readerly.loadsheet.dev/guide/renderers). Renderers are synchronous and pure.

`out.profile.color` already follows `--color`, `--no-color`, `FORCE_COLOR`, `NO_COLOR` and the TTY, see [color](https://readerly.loadsheet.dev/guide/color). Passing it on keeps the page plain in a pipe or a CI log.

## What each reader gets

The page in these samples:

```md
# ACME001

Route handler reads `process.env` at module scope.

The value is fixed at build time. Read it inside the handler.
```

At a terminal, `text` prints the verdict line, the rendered page, then the `next:` lines, separated by blank lines.

A coding agent gets the Markdown unchanged after the verdict line. Agents read Markdown well, and escape codes would only cost characters:

```text
acme explain: ok. no problems.
# ACME001

Route handler reads `process.env` at module scope.

The value is fixed at build time. Read it inside the handler.
next: acme check   (finds every ACME001)
```

The page counts against the agent's [budget](https://readerly.loadsheet.dev/guide/budget) of 8,000 characters, line by line. A page of 400 lines kept 144 and ended with:

```text
next: acme check   (finds every ACME001)
hidden: 256 lines. rerun: acme explain ACME001 --full, or acme explain ACME001 --format json
```

When a command writes an answer, the agent format prints the answer in place of diagnostics and log lines. The verdict line still counts them, so `acme explain: ok. 1 warning.` tells the agent there was one.

`--format json` adds the page as `answer` to the document, next to the diagnostics:

```json
{
  "schema": "acme.explain/v1",
  "status": "ok",
  "summary": {
    "error": 0,
    "warn": 0,
    "info": 0
  },
  "diagnostics": [],
  "result": {},
  "next": [
    {
      "command": "acme check",
      "reason": "finds every ACME001"
    }
  ],
  "answer": "# ACME001\n\nRoute handler reads `process.env` at module scope.\n\nThe value is fixed at build time. Read it inside the handler.\n"
}
```

`--format jsonl` sends it as an `answer` record before the `result` record:

```text
{"type":"version","schema":"acme.explain/v1","tool":"acme"}
{"type":"next","message":"acme check","reason":"finds every ACME001"}
{"type":"answer","message":"# ACME001\n\nRoute handler reads `process.env` at module scope.\n\nThe value is fixed at build time. Read it inside the handler.\n"}
{"type":"result","status":"ok","message":"no problems","data":{}}
```

## Calling write more than once

`out.write()` keeps one answer. A second call replaces the first. Build the whole page, then write it. Before a prompt, readerly prints what you wrote so far. A `write` after the prompt prints again at `end()`, even when the text is the same.

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

- [Overview](https://readerly.loadsheet.dev/raw/integrations.md)
- [GitHub Actions](https://readerly.loadsheet.dev/raw/integrations/github-actions.md)
- [Argument parsers](https://readerly.loadsheet.dev/raw/integrations/argument-parsers.md)
- [Testing your CLI](https://readerly.loadsheet.dev/raw/integrations/testing.md)
- [Progress](https://readerly.loadsheet.dev/raw/integrations/progress.md)
- [Markdown answers](https://readerly.loadsheet.dev/raw/integrations/markdown-answers.md) (this page)
- [nostics](https://readerly.loadsheet.dev/raw/integrations/nostics.md)

Next: [nostics](https://readerly.loadsheet.dev/raw/integrations/nostics.md)

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