qwen-code/docs/users/features/markdown-rendering.md
ChiGao 7f0c9791b7
Some checks are pending
Qwen Code CI / Lint (push) Waiting to run
Qwen Code CI / Test (push) Blocked by required conditions
Qwen Code CI / Test-1 (push) Blocked by required conditions
Qwen Code CI / Test-2 (push) Blocked by required conditions
Qwen Code CI / Test-3 (push) Blocked by required conditions
Qwen Code CI / Test-4 (push) Blocked by required conditions
Qwen Code CI / Test-5 (push) Blocked by required conditions
Qwen Code CI / Test-6 (push) Blocked by required conditions
Qwen Code CI / Test-7 (push) Blocked by required conditions
Qwen Code CI / Test-8 (push) Blocked by required conditions
Qwen Code CI / Post Coverage Comment (push) Blocked by required conditions
Qwen Code CI / CodeQL (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:docker (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:none (push) Waiting to run
E2E Tests / E2E Test - macOS (push) Waiting to run
SDK Python / SDK Python (3.10) (push) Waiting to run
SDK Python / SDK Python (3.11) (push) Waiting to run
SDK Python / SDK Python (3.12) (push) Waiting to run
feat(cli): expand TUI markdown rendering (#3680)
* feat(cli): expand markdown rendering in tui

* fix(cli): render finalized mermaid images synchronously

* fix(cli): harden markdown rendering paths

* fix(cli): preserve mermaid source fallbacks

* test(cli): make mermaid image renderer mock cross-platform

* fix(cli): run windows mmdc shims through shell

* fix(cli): address markdown rendering review comments

* fix(cli): validate mermaid render timeout

* feat(cli): expose rendered markdown source blocks

* fix(cli): align mermaid source copy controls

* fix(cli): make markdown render toggle visible

* fix(cli): keep markdown render toggle quiet

* fix(cli): generalize markdown render mode

* fix(cli): broaden render mode and copy latex blocks

* fix(cli): align rendered copy source indices

* fix(cli): support copying inline latex expressions

* feat(cli): document markdown render controls

* test(cli): cover markdown render controls

* fix(cli): tighten markdown render fallbacks

* fix(cli): bound mermaid renderer cache

* fix(cli): address markdown render review feedback

* fix(cli): address markdown render review comments

* test(cli): strengthen render mode shortcut coverage

* fix(cli): address mermaid image review comments

* fix(cli): stabilize renderer output truncation

* test(cli): flush fake renderer stderr before exit

* fix(cli): address markdown renderer review feedback

* docs(cli): clarify mermaid image limits

* fix(cli): refresh mermaid images on height resize

* fix(cli): address markdown render review

* chore: revert unrelated review formatting churn

* fix(cli): avoid mermaid regex codeql alert

* fix(cli): silence mermaid operator codeql alert

* fix(cli): render inline math in markdown tables

* fix(cli): harden markdown visual renderers

* fix(cli): strip c1 controls from mermaid previews

---------

Co-authored-by: 秦奇 <gary.gq@alibaba-inc.com>
2026-05-07 16:24:13 +08:00

163 lines
7.3 KiB
Markdown

# Markdown Rendering
Qwen Code renders common Markdown structures directly in the TUI so model
answers are easier to scan without leaving the terminal. The renderer is
designed to keep the original source reachable, especially for visual blocks
such as Mermaid diagrams and LaTeX math.
## Render and Raw Modes
By default, Markdown is shown in `render` mode. Supported blocks render as
visual previews where possible:
- Mermaid fenced code blocks
- Markdown tables
- task lists
- blockquotes
- inline and block LaTeX math
- fenced code blocks with syntax highlighting
Press `Alt/Option+M` to toggle the current session between modes. On macOS,
the terminal must send Option as Meta for this shortcut; otherwise Option+M is
treated as normal text input.
- `render`: show rich terminal previews for supported Markdown.
- `raw`: show source-oriented Markdown for visual blocks such as Mermaid,
tables, and LaTeX.
To start Qwen Code in raw mode by default, set `ui.renderMode`:
```json
{
"ui": {
"renderMode": "raw"
}
}
```
Accepted values are `"render"` and `"raw"`. The shortcut only changes the
current session view; it does not rewrite your settings file.
## Mermaid
Fenced `mermaid` code blocks render visually in `render` mode. The TUI uses a
layered strategy:
1. If enabled and supported, Qwen Code asks Mermaid CLI (`mmdc`) to render the
diagram to a PNG and sends it to the terminal image protocol.
2. If terminal images are unavailable but `chafa` is installed, the same PNG can
be converted to ANSI block graphics.
3. Otherwise, Qwen Code falls back to a terminal wireframe or compact text
preview.
4. If a Mermaid diagram type cannot be previewed, Qwen Code shows the original
fenced source instead of hiding it behind a placeholder.
Mermaid image rendering is disabled by default because it requires external
renderers and terminal image support. Enable it with:
```bash
QWEN_CODE_MERMAID_IMAGE_RENDERING=1 qwen
```
Optional environment variables:
| Variable | Description |
| ------------------------------------------- | ----------------------------------------------------------------------------------- |
| `QWEN_CODE_MERMAID_IMAGE_RENDERING=1` | Enables external Mermaid image rendering. |
| `QWEN_CODE_DISABLE_MERMAID_IMAGES=1` | Disables Mermaid image rendering even when enabled elsewhere. |
| `QWEN_CODE_MERMAID_IMAGE_PROTOCOL=kitty` | Forces Kitty protocol output. Useful for terminals such as Kitty and Ghostty. |
| `QWEN_CODE_MERMAID_IMAGE_PROTOCOL=iterm2` | Requests iTerm2 inline images. Interactive TUI rendering falls back to text/ANSI. |
| `QWEN_CODE_MERMAID_IMAGE_PROTOCOL=off` | Disables terminal image protocols and allows text or `chafa` fallback. |
| `QWEN_CODE_MERMAID_MMD_CLI=/path/to/mmdc` | Uses a specific Mermaid CLI executable. |
| `QWEN_CODE_MERMAID_ALLOW_NPX=1` | Allows Qwen Code to run `npx @mermaid-js/mermaid-cli` when `mmdc` is not installed. |
| `QWEN_CODE_MERMAID_ALLOW_LOCAL_RENDERERS=1` | Allows project-local renderer binaries under `node_modules/.bin`. |
| `QWEN_CODE_MERMAID_RENDER_WIDTH=1200` | Overrides the PNG render width. |
| `QWEN_CODE_MERMAID_RENDER_TIMEOUT_MS=10000` | Overrides the external render timeout, capped at 60000 ms. |
| `QWEN_CODE_MERMAID_CELL_ASPECT_RATIO=0.5` | Adjusts image row fitting for terminal font cell geometry. |
The first image render can be slow, especially when `npx` needs to resolve or
download Mermaid CLI. During streaming, Qwen Code shows a bounded text preview
and attempts image rendering only after the model response is complete.
### Mermaid Source Copy
Every rendered Mermaid block includes a source hint such as:
```text
Mermaid flowchart (TD) · source: /copy mermaid 1
```
Use these commands to copy Mermaid source from the last AI response:
| Command | Behavior |
| ---------------------- | --------------------------------------------- |
| `/copy mermaid` | Copies the last Mermaid block. |
| `/copy mermaid 1` | Copies the first Mermaid block. |
| `/copy code mermaid` | Copies the last fenced `mermaid` code block. |
| `/copy code mermaid 1` | Copies the first fenced `mermaid` code block. |
`/copy code 1` counts all fenced code blocks, not only Mermaid blocks. Use
`/copy mermaid N` when you want the Mermaid-specific sequence shown in the
rendered title.
## LaTeX Math
Qwen Code supports basic inline and block LaTeX rendering in the terminal:
```markdown
Inline math: $x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$
$$
\sum_{n=1}^{\infty} 1/n^2 = \pi^2/6
$$
```
The renderer focuses on common symbols and readable terminal output. It is not
a full TeX engine; complex layouts such as matrices, aligned equations, and
large nested expressions may be simplified.
Inline `$...$` expressions are intentionally bounded to 1024 characters per
line so malformed or very large generated Markdown cannot stall terminal
rendering. Longer formulas remain visible as source text and can still be
copied from raw mode or the original response.
### LaTeX Source Copy
Use these commands to copy LaTeX source from the last AI response:
| Command | Behavior |
| ---------------------- | --------------------------------------- |
| `/copy latex` | Copies the last block LaTeX expression. |
| `/copy latex 2` | Copies the second block expression. |
| `/copy latex inline` | Copies the last inline expression. |
| `/copy latex inline 2` | Copies the second inline expression. |
| `/copy inline-latex 2` | Alias for `/copy latex inline 2`. |
Inline LaTeX does not show a per-expression copy hint in rendered text to avoid
making prose noisy. Switch to raw mode with `Alt/Option+M` when you want to
inspect inline source in place; on macOS this requires Option-as-Meta terminal
input.
## General Code Copy
The `/copy code` command reads fenced code blocks from the last AI Markdown
response:
| Command | Behavior |
| ----------------------- | ---------------------------------------- |
| `/copy code` | Copies the last fenced code block. |
| `/copy code 2` | Copies the second fenced code block. |
| `/copy code typescript` | Copies the last `typescript` code block. |
| `/copy code mermaid 1` | Copies the first `mermaid` code block. |
## Current Limits
- Mermaid image rendering depends on Mermaid CLI plus terminal image support.
- Async iTerm2 inline image placement is disabled in the TUI because the
protocol is cursor-position bound; use Kitty/Ghostty or ANSI fallback for
interactive image previews.
- Wireframe Mermaid rendering is a readable terminal preview, not a full
Mermaid layout engine.
- Raw mode is global for rendered Markdown blocks; it is not a per-block toggle.
- LaTeX rendering covers common symbols and expressions, not full TeX layout.
- Source copy commands operate on the last AI response.