docs/design/markdown-syntax-extension.md
This document keeps the implementation references for the integrated Markdown
syntax extension PR. It is based on the TUI optimization research from
origin/docs/tui-optimization-design, especially:
docs/design/tui-optimization/00-overview.mddocs/design/tui-optimization/03-rendering-extensibility.mddocs/design/tui-optimization/04-gemini-cli-research.mddocs/design/tui-optimization/05-claude-code-research.mddocs/design/tui-optimization/06-implementation-rollout-checklist.mddocs/design/tui-optimization/08-execution-plan-and-test-matrix.mdThe referenced research recommends a long-term Markdown architecture built around an AST parser, block/token caching, stable-prefix streaming, bounded detail panels, and terminal capability detection. This first implementation keeps the runtime footprint small and makes the new behavior visible immediately.
This PR treats Markdown syntax expansion as one coherent renderer improvement, not separate feature PRs.
Included in the first implementation:
mmdc is available, and the terminal supports an image
path.flowchart / graph Mermaid diagrams fall back to box-and-arrow previews.sequenceDiagram Mermaid diagrams fall back to participant-arrow previews.classDiagram, stateDiagram, erDiagram, gantt, pie,
journey, mindmap, gitGraph, and requirementDiagram blocks fall back
to bounded text previews.$...$ math and block $$...$$ math render with common Unicode
substitutions.TableRenderer.CodeColorizer./copy mermaid N,
/copy latex N, /copy latex inline N, and raw mode.ui.renderMode controls whether sessions start in rendered or raw/source
mode, while Alt/Option+M toggles the active session view.The implementation now treats Mermaid's own layout as the preferred path. When the local environment supports it, the TUI renders Mermaid blocks through this pipeline:
Mermaid source
-> mmdc / Mermaid CLI
-> PNG
-> Kitty or iTerm2 terminal image protocol
If the terminal does not support inline images but chafa is installed, the
same PNG is rendered as ANSI block graphics. If neither image protocol nor
chafa is available, the renderer falls back to the synchronous terminal text
preview described below.
The image render is not attempted while a response is still streaming. During
streaming, Mermaid blocks show a bounded pending preview. Once the response is
finalized, the image path is attempted only when explicitly enabled. This keeps
slow mmdc startup, especially the opt-in npx path, out of the default
interactive render path.
PNG generation is cached independently from terminal placement. Repeated renders of the same Mermaid source, including terminal resize updates, reuse the generated PNG and only recompute the Kitty/iTerm2 placement dimensions.
The image path is intentionally opt-in and capability-gated instead of always
bundling or invoking Puppeteer/Chromium from the hot CLI path. A user can enable
the image path with QWEN_CODE_MERMAID_IMAGE_RENDERING=1, then provide
@mermaid-js/mermaid-cli by installing mmdc on PATH or by setting
QWEN_CODE_MERMAID_MMD_CLI to the binary path. For ad-hoc local verification,
QWEN_CODE_MERMAID_ALLOW_NPX=1 allows the renderer to invoke
npx -y @mermaid-js/[email protected]; this is intentionally opt-in because
the first run may install Puppeteer/Chromium and block rendering. Repo-local
node_modules/.bin renderers are not auto-discovered unless
QWEN_CODE_MERMAID_ALLOW_LOCAL_RENDERERS=1 is set. Terminal protocol selection
can be forced with QWEN_CODE_MERMAID_IMAGE_PROTOCOL=kitty|iterm2|off.
For Kitty-compatible terminals such as Ghostty, the renderer uses Kitty
Unicode placeholders instead of writing the image payload as Ink text. The PNG
is transmitted through raw stdout in quiet mode (q=2) with a virtual
placement (U=1), and the React tree renders the normal placeholder character
grid (U+10EEEE) with explicit row and column diacritics for each cell. This
keeps Ink responsible for layout and resize while preventing APC payload bytes
from being wrapped into visible base64 text.
The fallback avoids async work because Ink's <Static> path is append-only: a
finalized message cannot reliably wait for a background render job and then
update in place without forcing a full static refresh. The fallback must
therefore produce terminal output during the normal React render pass.
For flowchart / graph diagrams, the fallback builds a lightweight graph
model instead of printing one edge at a time:
\n / line breaks.[Yes], [No], [是], and [否].Cycles: section with explicit
↩ to <node> markers. This avoids unstable long cross-diagram routes in
terminal fonts while keeping the loop semantics visible.contentWidth, so resize changes node width,
spacing, and connector paths.Example:
flowchart LR
A[Client] --> B[API]
renders as a terminal visual preview rather than Mermaid source.
Other common Mermaid diagram families use bounded text summaries rather than a
full layout engine: class relationships/members, state transitions, ER
entities/relationships, Gantt tasks, pie slices, journey steps, mindmap trees,
git graph entries, and requirement trees. If a diagram type is unknown or not
previewable, the renderer shows the original fenced Mermaid source rather than
a placeholder so the content remains readable and selectable/copyable in the
terminal. Rendered Mermaid headings also show the Mermaid-specific copy command,
for example /copy mermaid 2, so users can recover the original diagram source
without switching the whole view to raw mode.
The fallback is still not a complete Mermaid engine. It is a fast, dependency-light preview layer for common LLM-generated diagrams when high-fidelity rendering is not available.
The provider boundary is intentionally open for additional native image providers:
mmdc / @mermaid-js/mermaid-cli for SVG/PNG output.terminal-image for Kitty/iTerm2 plus ANSI fallback.chafa when present for Sixel/Kitty/iTerm2/Unicode mosaics.This path should remain optional, cached, and capability-gated, with cache keys based on source hash, terminal width, renderer provider, and terminal protocol. It should not block startup or add bundled Mermaid/Puppeteer work to the hot TUI path by default.
The first version extends the existing parser to minimize blast radius. The
feature boundaries are still compatible with a future marked token pipeline:
code(lang=mermaid) -> MermaidDiagramcode(lang=*) -> existing CodeColorizertable -> existing TableRendererblockquote -> quote block rendererlist(task=true) -> task list rendererparagraph/text -> inline renderer with math/link/style supportThe implementation does not cache React nodes. A future AST renderer should cache tokens/blocks, then render from current width/theme/settings props.
Targeted unit verification:
cd packages/cli
npx vitest run \
src/config/settingsSchema.test.ts \
src/ui/AppContainer.test.tsx \
src/ui/utils/MarkdownDisplay.test.tsx \
src/ui/utils/mermaidImageRenderer.test.ts \
src/ui/commands/copyCommand.test.ts \
src/ui/components/BaseTextInput.test.tsx \
src/ui/keyMatchers.test.ts \
src/ui/contexts/KeypressContext.test.tsx
Broader verification before PR submission:
npm run build --workspace=packages/cli
npm run typecheck --workspace=packages/cli
npm run lint --workspace=packages/cli
git diff --check
Terminal-capture integration scenario:
npm run build && npm run bundle
cd integration-tests/terminal-capture
npm run capture:markdown-rendering
This scenario captures a Markdown-heavy model response, toggles raw/source mode
with Alt/Option+M, and verifies the visible source copy flows with
/copy mermaid 1 and /copy latex 1.
Manual scenarios:
flowchart LR block.sequenceDiagram block.ui.renderMode: "raw" starts a session in source-oriented mode.Alt/Option+M toggles the same response between rendered and raw/source
mode./copy mermaid N and /copy latex N source order.