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.
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
Complete HTML document
sbHtmlis fully replaced on each call.- Emits DOCTYPE,
<html>,<head>, and enclosing<body>by default. - Ideal for one-shot rendering: docs, emails, reports, previews.
- Optional
copyButtonfor code blocks.
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.
max-width.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.
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.
| Key | Type | Purpose |
|---|---|---|
theme | string | Predefined theme: ChatGPT, cleanWin, cleanMac, or raw. Omit for the default document. |
streaming | bool | Enables streaming mode — HTML is appended rather than replacing the destination. |
usesPrism | bool | Include PrismJS syntax highlighting for code blocks. |
prism.theme | string | Prism color theme (e.g. tomorrow, okaidia, twilight). |
prism.version | string | Prism version, e.g. 1.29.0 (the default). |
copyButton | bool | Add a copy button for code blocks (on by default in streaming mode). |
ChatGPT.max-width | string | Body text max width for the ChatGPT theme, using CSS length units such as 80ch. |
emitJavascript v11.3.0 | bool | Emit JavaScript function calls instead of HTML to update an embedded browser live. |
streamingShell v11.3.0 | bool | Convert an empty string into a styled HTML shell — the starting page for JavaScript updates. |
docType | string | Override the DOCTYPE (default <!DOCTYPE html>). |
rootElement | string | Override the root html element (default <html>). |
head | string | Override the head section. Prism links are inserted before </head> when enabled. |
bodyStart / bodyEnd | string | Override the opening/closing body fragments (default wrap content in <div id="content">). |
noContentDiv | bool | Omit 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.
// 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.
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
Generate the HTML shell with streamingShell and load it into the WebView.
Incoming chunks are analyzed — formatting vs. raw content.
Formatting → appendHtmlBySelector. Plain text → appendTextBySelector.
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.
Sample code
Markdown examples
Complete, runnable examples — shown here in C#, and available in 30+ languages.
💻 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.
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.