GitHub
Skip to content

Markdown answers

Render a command's Markdown answer with @comark/ansi for a person at a terminal, and pass it as is to coding agents, json and jsonl.

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

@comark/ansi turns Markdown into terminal text. Render only when the format is text, and pass the Markdown as is to everyone else:

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();
}
explain.ts

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. Renderers are synchronous and pure.

out.profile.color already follows --color, --no-color, FORCE_COLOR, NO_COLOR and the TTY, see color. Passing it on keeps the page plain in a pipe or a CI log.

The page in these samples:

# ACME001

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

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

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:

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)
text

The page counts against the agent's budget of 8,000 characters, line by line. A page of 400 lines kept 144 and ended with:

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

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:

{
  "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"
}
json

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

{"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":{}}
text

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.