Skip to content

Fixes #5385. Add Markdown.RenderToAnsi() API for headless ANSI rendering#5388

Merged
tig merged 3 commits into
developfrom
tig/add-markdown-render-to-ansi-api
May 23, 2026
Merged

Fixes #5385. Add Markdown.RenderToAnsi() API for headless ANSI rendering#5388
tig merged 3 commits into
developfrom
tig/add-markdown-render-to-ansi-api

Conversation

@tig
Copy link
Copy Markdown
Member

@tig tig commented May 23, 2026

Summary

Adds a public RenderToAnsi instance method on the Markdown view that renders markdown content to ANSI escape sequences without requiring Application.Init() or an interactive TUI session.

API

// Instance method - reuses view configuration (SyntaxHighlighter, Pipeline, etc.)
Markdown md = new () { SyntaxHighlighter = new TextMateSyntaxHighlighter () };
string ansi = md.RenderToAnsi (markdownText, width: Console.WindowWidth);

Parameters

  • markdown (optional) — Text to render. If null, uses the current Text property.
  • width (optional) — Target column width for word-wrapping. Defaults to 80.

How it works

The method encapsulates the ~20-line headless driver workaround described in the issue into a single call:

  1. Sets DisableRealDriverIO=1 to prevent real terminal I/O
  2. Creates a temporary IApplication with the ANSI driver
  3. Creates a fresh Markdown view copying configuration from the calling instance
  4. Performs layout to determine content height
  5. Resizes the buffer to the full document height and draws
  6. Extracts and returns the ANSI output via Driver.ToAnsi()
  7. Cleans up the temporary application

Configuration properties respected

  • SyntaxHighlighter — Syntax highlighting for code blocks
  • MarkdownPipeline — Custom Markdig pipeline
  • UseThemeBackground — Theme background colors
  • ShowHeadingPrefix — Whether to show # prefixes

Testing

  • 9 new unit tests covering basic rendering, width wrapping, syntax highlighting, edge cases, and non-mutation of the calling instance
  • All 374 existing Markdown tests continue to pass

Closes #5385

Add a public instance method on the Markdown view that renders markdown
content to ANSI escape sequences without requiring Application.Init() or
an interactive TUI session. The method encapsulates the headless driver
workaround into a single call:

  string ansi = markdownView.RenderToAnsi(text, width: 80);

The method:
- Creates a temporary headless ANSI driver internally
- Copies SyntaxHighlighter, MarkdownPipeline, UseThemeBackground, and
  ShowHeadingPrefix configuration from the instance
- Calculates full content height and renders the entire document
- Returns the ANSI string without mutating the calling instance

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9757f7eb22

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Terminal.Gui/Views/Markdown/MarkdownView.Ansi.cs Outdated
Wrap the temporary IApplication and Markdown view in using declarations
so they are disposed on all paths, including when Layout(), Draw(), or
a custom ISyntaxHighlighter throws an exception.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new public API on Terminal.Gui.Views.Markdown to render Markdown content into an ANSI-escaped string in a headless context (no interactive TUI session), enabling “cat/print mode” scenarios like those described in #5385.

Changes:

  • Added Markdown.RenderToAnsi(...) instance method that bootstraps a temporary ANSI-driver IApplication, lays out/draws a configured Markdown view, and returns Driver.ToAnsi() output.
  • Added parallelizable unit tests validating basic rendering, parameter behavior, width wrapping, syntax highlighting, and non-mutation of the calling instance.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
Terminal.Gui/Views/Markdown/MarkdownView.Ansi.cs Implements the new RenderToAnsi headless ANSI rendering API on Markdown.
Tests/UnitTestsParallelizable/Views/Markdown/MarkdownRenderToAnsiTests.cs Adds unit tests covering the new ANSI-rendering API behavior.

Comment thread Terminal.Gui/Views/Markdown/MarkdownView.Ansi.cs
Comment thread Terminal.Gui/Views/Markdown/MarkdownView.Ansi.cs Outdated
Comment thread Terminal.Gui/Views/Markdown/MarkdownView.Ansi.cs Outdated
Comment thread Terminal.Gui/Views/Markdown/MarkdownView.Ansi.cs Outdated
Comment thread Terminal.Gui/Views/Markdown/MarkdownView.Ansi.cs Outdated
- Add MAX_RENDER_WIDTH (4096) upper bound to prevent OOM from absurd widths
- Wrap env var set/restore in a static lock for thread safety
- Use initial height of 1 instead of width for first layout pass
- Fix XML docs to clarify ShowCopyButtons is always disabled for ANSI output

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tig tig requested review from BDisp, YourRobotOverlord and harder May 23, 2026 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Markdown-to-ANSI rendering API on Markdown view

3 participants