Zig Requires Chilkat v11.2.0+
Zig
Markdown to HTML - Streaming Mode
See more Markdown Examples
Shows how to convert Markdown to HTML in streaming mode, ideal for processing deltas with fragments of incoming Markdown during AI streaming responses. This example will simulate processing incoming AI delta's by streaming 80-byte chunks from a markdown file.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const options = try chilkat.JsonObject.init();
defer options.deinit();
options.updateString("theme", "raw") catch {};
// Indicate streaming mode
options.updateBool("streaming", true) catch {};
// Load the full markdown file, which will be converted to HTML 80 bytes at a time in streaming mode.
// The sample markdown input for this example is identical to the one at Markdown to HTML - Full Document, Raw, where you can view it.
const sb_full_markdown = try chilkat.StringBuilder.init();
defer sb_full_markdown.deinit();
sb_full_markdown.loadFile("qa_data/markdown/test1.md", "utf-8") catch {
std.debug.print("{s}\n", .{try sb_full_markdown.getLastErrorText(alloc)});
return;
};
// This will contain the fragment of HTMl produced at each step in streaming mode.
const sb_html_frag = try chilkat.StringBuilder.init();
defer sb_html_frag.deinit();
// We'll accumulate the full HTML result here, by appending each HTLM fragment
const sb_html = try chilkat.StringBuilder.init();
defer sb_html.deinit();
// This will contain the chunk of markdown not yet converted to HTML.
const sb_streaming_markdown = try chilkat.StringBuilder.init();
defer sb_streaming_markdown.deinit();
// This contains the current 80 byte markdown chunk to be processed.
// The last chunk can be less than 80 bytes.
const sb_md_chunk = try chilkat.StringBuilder.init();
defer sb_md_chunk.deinit();
// Let's safeguard the loop to process a max of 160K of markdown (2000 x 80 bytes = 160K)
// This is to prevent an infinite loop in case something isn't working correctly.
var safety: i32 = 0;
while ((sb_full_markdown.getLength() > 0) and (safety < 2000)) {
var chunk_size: i32 = 80;
if (sb_full_markdown.getLength() < chunk_size) {
chunk_size = sb_full_markdown.getLength();
}
// Remove the leading chunk from the sbFullMarkdown and append to sbStreamingMarkdown.
const s = try sb_full_markdown.getRange(alloc, 0, chunk_size, true);
sb_streaming_markdown.append(s) catch {};
std.debug.print("-------------------------------------------------------------\n", .{});
sb_streaming_markdown.toCRLF() catch {};
std.debug.print("{s}\n", .{try sb_streaming_markdown.getAsString(alloc)});
// Convert what is possible and append to the HTML fragment.
// It's possible that no additional HTML can be generated if the markdown chunk is does not contain even a single full line.
// In streaming mode, MarkdownToHtml appends any additional HTML it can generate to the StringBuilder passed in the 2nd argument.
// If we want to only see what new HTML was generated from this chunk, we need to clear the StringBuilder before calling MarkdownToHtml.
// The markdown that was converted is removed from the caller's contents.
sb_html_frag.clear();
sb_streaming_markdown.markdownToHtml(options, sb_html_frag) catch {};
// Accumulate the HTML fragments into a full HTML document.
sb_html.appendSb(sb_html_frag) catch {};
std.debug.print("----\n", .{});
sb_html_frag.toCRLF() catch {};
std.debug.print("{s}\n", .{try sb_html_frag.getAsString(alloc)});
safety = safety + 1;
}
// Flush any remaining closing HTML tags by passing a final line-ending.
std.debug.print("----FINAL----------------------------------------------------\n", .{});
sb_streaming_markdown.append("\n") catch {};
std.debug.print("{s}\n", .{try sb_streaming_markdown.getAsString(alloc)});
sb_html_frag.clear();
sb_streaming_markdown.markdownToHtml(options, sb_html_frag) catch {};
sb_html.appendSb(sb_html_frag) catch {};
std.debug.print("----\n", .{});
std.debug.print("{s}\n", .{try sb_html_frag.getAsString(alloc)});
std.debug.print("\n", .{});
std.debug.print("**** Full Accumulated HTML ****\n", .{});
sb_html.toCRLF() catch {};
std.debug.print("{s}\n", .{try sb_html.getAsString(alloc)});
// Notice how the 1st loop iteration produces no HTML output. This is because there is not yet a complete line of markdown to process.
// Sample ouput:
// -------------------------------------------------------------
// Here’s a simple implementation of the standard C library function `strncpy`, wri
// ----
//
// -------------------------------------------------------------
// Here’s a simple implementation of the standard C library function `strncpy`, written from scratch — demonstrating how it works internally:
//
// ```c
// #include <st
// ----
// <p>Here’s a simple implementation of the standard C library function <code>strncpy</code>, written from scratch — demonstrating how it works internally:</p>
//
// -------------------------------------------------------------
// #include <stddef.h> // for size_t
//
// char *my_strncpy(char *dest, const char *src, size_t n
// ----
// <pre id="pre_dd232b5f-2279-4841-b602-5840072f7a84"><code id="dd232b5f-2279-4841-b602-5840072f7a84" class="language-c">#include <stddef.h> // for size_t
//
//
// -------------------------------------------------------------
// char *my_strncpy(char *dest, const char *src, size_t n)
// {
// size_t i;
//
// for (i = 0; i < n && src[i] != '\0'; i++) {
//
// ----
// char *my_strncpy(char *dest, const char *src, size_t n)
// {
// size_t i;
//
// for (i = 0; i < n && src[i] != '\0'; i++) {
//
// -------------------------------------------------------------
// dest[i] = src[i];
// }
//
// // If src is shorter than n, pad with '\0'
//
// ----
// dest[i] = src[i];
// }
//
// // If src is shorter than n, pad with '\0'
//
// -------------------------------------------------------------
// for (; i < n; i++) {
// dest[i] = '\0';
// }
//
// return dest;
// }
// ```
// ----
// for (; i < n; i++) {
// dest[i] = '\0';
// }
//
// return dest;
// }
//
// -------------------------------------------------------------
// ```
//
// ### Explanation:
//
// * **Parameters:**
//
// * `dest`: destination buffer to c
// ----
// </code></pre>
// <h3>Explanation:</h3>
// <ul>
// <li><strong>Parameters:</strong>
// -------------------------------------------------------------
// * `dest`: destination buffer to copy into.
// * `src`: source string.
// * `n`: maximum number of bytes to copy.
//
// ----
// <ul>
// <li><code>dest</code>: destination buffer to copy into.</li>
// <li><code>src</code>: source string.</li>
// <li><code>n</code>: maximum number of bytes to copy.
// -------------------------------------------------------------
//
//
// * **Logic:**
//
// 1. Copy characters from `src` into `dest` until you hit eit
// ----
// </li>
// </ul>
// </li>
// <li><strong>Logic:</strong>
// -------------------------------------------------------------
// 1. Copy characters from `src` into `dest` until you hit either:
//
// * The null terminator (`'\0'`) in `src`, or
// * The limit `n`.
//
// ----
// <ol>
// <li>Copy characters from <code>src</code> into <code>dest</code> until you hit either:
// <ul>
// <li>The null terminator (<code>'\0'</code>) in <code>src</code>, or</li>
// <li>The limit <code>n</code>.
// -------------------------------------------------------------
//
// 2. If you reached the end of `src` before hitting `n`, pad the remaining byte
// ----
//
// -------------------------------------------------------------
// 2. If you reached the end of `src` before hitting `n`, pad the remaining bytes of `dest` with `'\0'`.
// 3. Return `dest` so it behaves like standard C libra
// ----
// </li>
// </ul>
// </li>
// <li>If you reached the end of <code>src</code> before hitting <code>n</code>, pad the remaining bytes of <code>dest</code> with <code>'\0'</code>.
// -------------------------------------------------------------
// 3. Return `dest` so it behaves like standard C library functions.
//
// ### Notes:
//
// * `strncpy` does **not** guarantee null-terminati
// ----
// </li>
// <li>Return <code>dest</code> so it behaves like standard C library functions.</li>
// </ol>
// </li>
// </ul>
// <h3>Notes:</h3>
//
// -------------------------------------------------------------
// * `strncpy` does **not** guarantee null-termination if `src` has length ≥ `n`.
// To ensure a null-terminated string, you can do:
// ----
// <ul>
// <li><code>strncpy</code> does <strong>not</strong> guarantee null-termination if <code>src</code> has length ≥ <code>n</code>.
// -------------------------------------------------------------
// To ensure a null-terminated string, you can do:
//
// ```c
// dest[n - 1] = '\0';
// ```
//
// — but only if you *always* reserve
// ----
//
// To ensure a null-terminated string, you can do:
// <pre id="pre_80ca4db3-f745-473c-91d7-a565d106bac8"><code id="80ca4db3-f745-473c-91d7-a565d106bac8" class="language-c">dest[n - 1] = '\0';
// </code></pre>
//
// -------------------------------------------------------------
// — but only if you *always* reserve space for that final null.
//
// ---
//
// Would you like me to also show a **safer**
// ----
// — but only if you <em>always</em> reserve space for that final null.</li>
// </ul>
// <hr />
//
// -------------------------------------------------------------
// Would you like me to also show a **safer** version (like how `strlcpy` behaves) that guarantees null-termination?
//
// ----
// <p>Would you like me to also show a <strong>safer</strong> version (like how <code>strlcpy</code> behaves) that guarantees null-termination?
// ----FINAL----------------------------------------------------
//
//
// ----
// </p>
// *******************************
// **** Full Accumulated HTML ****
// *******************************
// <p>Here’s a simple implementation of the standard C library function <code>strncpy</code>, written from scratch — demonstrating how it works internally:</p>
// <pre id="pre_dd232b5f-2279-4841-b602-5840072f7a84"><code id="dd232b5f-2279-4841-b602-5840072f7a84" class="language-c">#include <stddef.h> // for size_t
//
// char *my_strncpy(char *dest, const char *src, size_t n)
// {
// size_t i;
//
// for (i = 0; i < n && src[i] != '\0'; i++) {
// dest[i] = src[i];
// }
//
// // If src is shorter than n, pad with '\0'
// for (; i < n; i++) {
// dest[i] = '\0';
// }
//
// return dest;
// }
// </code></pre>
// <h3>Explanation:</h3>
// <ul>
// <li><strong>Parameters:</strong><ul>
// <li><code>dest</code>: destination buffer to copy into.</li>
// <li><code>src</code>: source string.</li>
// <li><code>n</code>: maximum number of bytes to copy.</li>
// </ul>
// </li>
// <li><strong>Logic:</strong><ol>
// <li>Copy characters from <code>src</code> into <code>dest</code> until you hit either:
// <ul>
// <li>The null terminator (<code>'\0'</code>) in <code>src</code>, or</li>
// <li>The limit <code>n</code>.</li>
// </ul>
// </li>
// <li>If you reached the end of <code>src</code> before hitting <code>n</code>, pad the remaining bytes of <code>dest</code> with <code>'\0'</code>.</li>
// <li>Return <code>dest</code> so it behaves like standard C library functions.</li>
// </ol>
// </li>
// </ul>
// <h3>Notes:</h3>
// <ul>
// <li><code>strncpy</code> does <strong>not</strong> guarantee null-termination if <code>src</code> has length ≥ <code>n</code>.
// To ensure a null-terminated string, you can do:
// <pre id="pre_80ca4db3-f745-473c-91d7-a565d106bac8"><code id="80ca4db3-f745-473c-91d7-a565d106bac8" class="language-c">dest[n - 1] = '\0';
// </code></pre>
// — but only if you <em>always</em> reserve space for that final null.</li>
// </ul>
// <hr />
// <p>Would you like me to also show a <strong>safer</strong> version (like how <code>strlcpy</code> behaves) that
}