⚡ Chilkat StringBuilder · Introduced in v11.2.0

Markdown → HTML
StringBuilder.MarkdownToHtml

Convert Markdown to clean, themed HTML directly inside your application — as a complete document or as a real-time stream. One method, identical across every Chilkat-supported programming language.

The Method

One call, two modes

The MarkdownToHtml method lives on Chilkat's StringBuilder class. It reads the Markdown held by the object and writes HTML into a destination StringBuilder. A single JSON options argument controls themes, syntax highlighting, streaming, and document structure.

method signature
public bool MarkdownToHtml(JsonObject options, StringBuilder sbHtml);
📑

Full-document

In non-streaming mode, sbHtml is fully replaced with a complete HTML document — DOCTYPE, head, body, and styling included.

🌐

Streaming

In streaming mode, only complete Markdown lines are converted and appended to sbHtml. Any trailing partial line is kept for the next chunk.

🤖

AI-ready

Built for live LLM output. Chilkat parses incoming Markdown deltas as a state machine and can emit JavaScript calls that update an embedded browser in real time.


How it behaves

Full document vs. streaming

NON-STREAMING

Complete HTML document

  • sbHtml is fully replaced on each call.
  • Emits DOCTYPE, <html>, <head>, and enclosing <body> by default.
  • Ideal for one-shot rendering: docs, emails, reports, previews.
  • Optional copyButton for code blocks.
STREAMING

Incremental fragments

  • Generated HTML is appended to sbHtml.
  • Only complete Markdown lines are converted; the partial final line stays buffered.
  • Converted Markdown is removed from the source object.
  • Perfect for processing AI response deltas as they arrive.

Presentation

Built-in themes

Select a predefined theme when creating a shell or converting in non-streaming mode. Or leave it empty for the default full-document output.

ChatGPTA style similar to the ChatGPT app. Supports max-width.
cleanWinA clean style for Windows applications.
cleanMacA clean style for macOS applications.
rawNo styles or wrapper elements — just the generated HTML.
defaultOmit theme for a full document with default doctype/head/body.

🎨 PrismJS code syntax highlighting

Set usesPrism: true to include PrismJS highlighting for fenced code blocks. Choose a Prism theme and version.

tomorrowdefaultcoydarkfunkyokaidiasolarizedlighttwilight

Prism version defaults to 1.29.0 when not specified.


Configuration

The options JSON

Everything is driven by keys in the options JsonObject. v11.2.0 and v11.3.0 mark when each key was introduced.

KeyTypePurpose
themestringPredefined theme: ChatGPT, cleanWin, cleanMac, or raw. Omit for the default document.
streamingboolEnables streaming mode — HTML is appended rather than replacing the destination.
usesPrismboolInclude PrismJS syntax highlighting for code blocks.
prism.themestringPrism color theme (e.g. tomorrow, okaidia, twilight).
prism.versionstringPrism version, e.g. 1.29.0 (the default).
copyButtonboolAdd a copy button for code blocks (on by default in streaming mode).
ChatGPT.max-widthstringBody text max width for the ChatGPT theme, using CSS length units such as 80ch.
emitJavascript v11.3.0boolEmit JavaScript function calls instead of HTML to update an embedded browser live.
streamingShell v11.3.0boolConvert an empty string into a styled HTML shell — the starting page for JavaScript updates.
docTypestringOverride the DOCTYPE (default <!DOCTYPE html>).
rootElementstringOverride the root html element (default <html>).
headstringOverride the head section. Prism links are inserted before </head> when enabled.
bodyStart / bodyEndstringOverride the opening/closing body fragments (default wrap content in <div id="content">).
noContentDivboolOmit the default content wrapper div from bodyStart/bodyEnd.

Quick start

Full document, default

An empty options object produces a complete HTML document — doctype, head, body, and all.

C# — identical pattern in every Chilkat language
// Leaving the options empty produces a full HTML document.
Chilkat.JsonObject options = new Chilkat.JsonObject();

Chilkat.StringBuilder sbMarkdown = new Chilkat.StringBuilder();
Chilkat.StringBuilder sbHtml = new Chilkat.StringBuilder();

sbMarkdown.LoadFile("qa_data/markdown/test1.md", "utf-8");

sbMarkdown.MarkdownToHtml(options, sbHtml);
sbHtml.ToCRLF();

Debug.WriteLine(sbHtml.GetAsString());

Streaming mode

Feed Markdown in chunks — exactly how AI deltas arrive. Each call appends whatever complete lines it can, and returns the rest to the buffer.

streaming loop (excerpt)
Chilkat.JsonObject options = new Chilkat.JsonObject();
options.UpdateString("theme", "raw");
options.UpdateBool("streaming", true);

// sbStreamingMarkdown holds Markdown not yet converted.
// Append each incoming chunk, then convert what's possible.
while (sbFullMarkdown.Length > 0) {
    string chunk = sbFullMarkdown.GetRange(0, 80, true);
    sbStreamingMarkdown.Append(chunk);

    sbHtmlFrag.Clear();
    sbStreamingMarkdown.MarkdownToHtml(options, sbHtmlFrag);  // appends new HTML
    sbHtml.AppendSb(sbHtmlFrag);
}

// Flush remaining closing tags with a final newline.
sbStreamingMarkdown.Append("\n");
sbHtmlFrag.Clear();
sbStreamingMarkdown.MarkdownToHtml(options, sbHtmlFrag);
sbHtml.AppendSb(sbHtmlFrag);

New in v11.3.0

Real-time streaming to embedded browsers

LLMs like GPT-4 emit rich Markdown token-by-token. Instead of making users wait for the whole answer, Chilkat acts as an intelligent bridge — parsing incoming Markdown as a state machine and translating it directly into JavaScript calls that update an embedded browser (or WebView) as the AI "thinks."

📜

streamingShell: true

Convert an empty string into a full HTML "shell" — CSS themes, layout containers, and the JavaScript functions needed to receive updates. Load it once into your WebView as a styled, empty stage.

emitJavascript: true

As deltas arrive, Chilkat emits calls to appendHtmlBySelector (for formatting like <li> or code blocks) and appendTextBySelector (for plain content) — executed immediately by the browser.

How it flows

Prepare the shell

Generate the HTML shell with streamingShell and load it into the WebView.

Parse Markdown

Incoming chunks are analyzed — formatting vs. raw content.

Select a function

Formatting → appendHtmlBySelector. Plain text → appendTextBySelector.

Execute live

The browser runs the calls instantly, building nested HTML and auto-scrolling.

By dynamically adjusting the CSS selector (for example div.response-content:last-of-type > ol:last-child), Chilkat builds complex, correctly-nested HTML in real time — letting users watch the answer format itself line by line instead of staring at a spinner.

💻 Full sample project on GitHub

A conversational AI interface in C# using Chilkat.Ai, demonstrating real-time Markdown streaming directly into an embedded browser. A short video demo is also available.

View on GitHub

Write once, everywhere

The same API in every language

StringBuilder.MarkdownToHtml is identical across all Chilkat-supported languages and platforms — the C# examples on this page translate directly.

C#C++CJavaPythonChilkat2-PythonNode.jsJavaScriptPHPPerlRubyGoSwiftObjective-CVB.NETVBScriptVisual Basic 6.0PowerShellPowerBuilderDelphiDataFlexXojoVisual FoxProPureBasicAutoItTclSQL ServerClassic ASPAndroid™