Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Visual Basic 6.0 Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Visual Basic 6.0) 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.

Note: This example requires Chilkat v11.2.0 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Dim success As Long
success = 0

Dim options As New ChilkatJsonObject
success = options.UpdateString("theme","raw")

' Indicate streaming mode
success = options.UpdateBool("streaming",1)

' 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.
Dim sbFullMarkdown As New ChilkatStringBuilder
success = sbFullMarkdown.LoadFile("qa_data/markdown/test1.md","utf-8")
If (success = 0) Then
    Debug.Print sbFullMarkdown.LastErrorText
    Exit Sub
End If

' This will contain the fragment of HTMl produced at each step in streaming mode.
Dim sbHtmlFrag As New ChilkatStringBuilder

' We'll accumulate the full HTML result here, by appending each HTLM fragment
Dim sbHtml As New ChilkatStringBuilder

' This will contain the chunk of markdown not yet converted to HTML.
Dim sbStreamingMarkdown As New ChilkatStringBuilder

' This contains the current 80 byte markdown chunk to be processed.
' The last chunk can be less than 80 bytes.
Dim sbMdChunk As New ChilkatStringBuilder

' 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.
Dim safety As Long
safety = 0

Do While (sbFullMarkdown.Length > 0) And (safety < 2000)
    Dim chunkSize As Long
    chunkSize = 80
    If (sbFullMarkdown.Length < chunkSize) Then
        chunkSize = sbFullMarkdown.Length
    End If

    ' Remove the leading chunk from the sbFullMarkdown and append to sbStreamingMarkdown.
    Dim s As String
    s = sbFullMarkdown.GetRange(0,chunkSize,1)
    success = sbStreamingMarkdown.Append(s)

    Debug.Print "-------------------------------------------------------------"
    success = sbStreamingMarkdown.ToCRLF()
    Debug.Print sbStreamingMarkdown.GetAsString()

    ' 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.
    sbHtmlFrag.Clear 
    success = sbStreamingMarkdown.MarkdownToHtml(options,sbHtmlFrag)

    ' Accumulate the HTML fragments into a full HTML document.
    success = sbHtml.AppendSb(sbHtmlFrag)

    Debug.Print "----"
    success = sbHtmlFrag.ToCRLF()
    Debug.Print sbHtmlFrag.GetAsString()

    safety = safety + 1
Loop

' Flush any remaining closing HTML tags by passing a final line-ending.
Debug.Print "----FINAL----------------------------------------------------"
success = sbStreamingMarkdown.Append(vbLf)
Debug.Print sbStreamingMarkdown.GetAsString()

sbHtmlFrag.Clear 
success = sbStreamingMarkdown.MarkdownToHtml(options,sbHtmlFrag)
success = sbHtml.AppendSb(sbHtmlFrag)

Debug.Print "----"
Debug.Print sbHtmlFrag.GetAsString()

Debug.Print ""
Debug.Print "**** Full Accumulated HTML ****"
success = sbHtml.ToCRLF()
Debug.Print sbHtml.GetAsString()

' 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 &lt;stddef.h&gt;  // 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 &lt; n &amp;&amp; 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 &lt; 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 &lt;stddef.h&gt;  // for size_t
' 
' char *my_strncpy(char *dest, const char *src, size_t n)
' {
'     size_t i;
' 
'     for (i = 0; i &lt; n &amp;&amp; src[i] != '\0'; i++) {
'         dest[i] = src[i];
'     }
' 
'     // If src is shorter than n, pad with '\0'
'     for (; i &lt; 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 

 

© 2000-2025 Chilkat Software, Inc. All Rights Reserved.