Apus converts outbound Markdown into a shared intermediate representation (IR) before rendering channel-specific output. The IR keeps plain text plus style/link spans, so one parse step feeds every channel and chunking never splits formatting mid-span.

Pipeline

  1. Parse Markdown into IR (markdownToIR) - plain text plus style spans (bold, italic, strikethrough, code, code block, spoiler, blockquote, heading 1-6) and link spans. Offsets are UTF-16 code units so Signal style ranges align with its API directly. Tables parse only when the channel opts into a table mode.
  2. Chunk the IR (chunkMarkdownIR / renderMarkdownIRChunksWithinLimit)
    • style and link spans are sliced with the text. The rendered-size chunker measures each candidate after channel escaping and link rewriting, and returns the accepted source slice together with its rendered payload.
  3. Render per channel (renderMarkdownWithMarkers) - a style-marker map turns spans into the channel's native markup.

Raw inline HTML lexemes retain their original bytes during parsing. Markdown markers and entities inside attribute values or recognized inline comments are not parsed as Markdown content. Non-serialized authored-tag ranges let HTML-aware renderers interpret those tags; other renderers keep them literal or escape them. HTML block parsing stays disabled so Markdown inside containers still works, and bare URLs in their bodies retain normal linkification.

Examples of shared IR renderers:

Channel Renderer Notes
Matrix HTML tags, including native tables Automatic replies, direct sends, and media captions use the same formatter
Signal plain text + text-style ranges Links render as label (url) when the label differs from the URL
Slack mrkdwn tokens (*bold*, _italic_, `code`, code fences) Links become <url|label>; autolink disabled during parse to avoid double-linking
Telegram HTML tags (<b>, <i>, <s>, <code>, <pre><code>, <a href>, <tg-spoiler>) Also supports rich-message tables and headings (<h1>-<h6>) when richMessages is on
WhatsApp WhatsApp style markers Uses shared IR; styled chunks are measured after rendering

IR example

Input Markdown:

Hello **world** - see [docs](https://ami-docs.dev.apuscorp.com).

IR (schematic):

{
  "text": "Hello world - see docs.",
  "styles": [{ "start": 6, "end": 11, "style": "bold" }],
  "links": [{ "start": 19, "end": 23, "href": "https://ami-docs.dev.apuscorp.com" }]
}

Table handling

markdown.tables controls how a channel converts Markdown tables, per channel and optionally per account:

Mode Behavior
code Render as an aligned ASCII table inside a code block (fallback default)
bullets Convert each row into label: value bullet points
block Keep native tables where the transport supports them; falls back to code otherwise
off Disable table parsing; raw table text passes through unchanged

Inline code in table cells keeps its parsed content, including leading and trailing spaces, in every enabled table mode.

Per-channel plugin defaults: Matrix defaults to block (native tables); Mattermost defaults to off; Signal and WhatsApp default to bullets; Telegram defaults to block (which resolves to code unless the account has richMessages enabled). Any channel without an explicit plugin default falls back to code.

channels:
  discord:
    markdown:
      tables: code
    accounts:
      work:
        markdown:
          tables: off

Chunking rules

See Streaming and chunking for chunk-boundary and delivery behavior across channels.

Link policy

Spoilers

Spoiler markers (||spoiler||) are parsed for Signal (mapped to SPOILER style ranges) and Telegram (mapped to <tg-spoiler>). Other channels treat ||...|| as plain text.

Collapsible details

The Control UI and Telegram accounts with richMessages: true render <details><summary>Label</summary> disclosures as native collapsible sections. Apus tells the model about this option only when the current reply surface supports it. Other channels, including Telegram accounts without rich messages, flatten each disclosure to **Summary** followed by the visible body so no content is hidden or lost.

Adding or updating a channel formatter

  1. Parse once with markdownToIR(...), passing channel-appropriate options (autolink, headingStyle, blockquotePrefix, tableMode).
  2. Render with renderMarkdownWithMarkers(...) and a style-marker map (or custom style-range logic for transports like Signal).
  3. Chunk with chunkMarkdownIR(...) or renderMarkdownIRChunksWithinLimit(...). The latter returns the measured rendered payload; use it directly instead of rendering the source again.
  4. Wire the adapter to call the new chunker and renderer from the outbound send path.
  5. Test with format tests plus an outbound delivery test if the channel chunks.

For disjoint text replacements and insertions, apus/plugin-sdk/text-chunking exports applyMarkdownTextEdits(text, edits). Edits use UTF-16 source coordinates; the result contains the edited text and mapOffset for boundaries outside replaced interiors. Each edit shifts offsets at and after its end. Insertions at the same position retain caller order and precede a replacement starting there. Renderers decide which styles include replacement text.

Common gotchas

Related

Streaming and chunking

Outbound streaming behavior, chunk boundaries, and channel-specific delivery.

System prompt

What the model sees before the conversation, including injected workspace files.