#Markdown to HTML bstr Renderer
#define FIO_MD2HTML
#include "fio-stl.h"A complete-document Markdown / GFM renderer that turns parser events into escaped HTML stored in an owned fio_bstr. Markdown goes in, tags come out. Implemented in ./103 md2html.h.
Defining FIO_MD2HTML automatically includes:
FIO_STRforfio_bstrFIO_GFMfor Markdown / GFM parsing
#Error Codes
#FIO_MD2HTML_ERR_ALLOC
#ifndef FIO_MD2HTML_ERR_ALLOC
#define FIO_MD2HTML_ERR_ALLOC 1
#endifInternal renderer error used when output allocation fails. Override before inclusion only if you need a different non-zero value.
#Rendering
#fio_md2html
SFUNC char *fio_md2html(char *bstr_target, fio_buf_info_s source);Renders a complete Markdown / GFM document into a fio_bstr.
Parameters:
bstr_target- existingfio_bstrto append to, orNULLto allocate a new onesource- Markdown bytes to parse
Returns: the output fio_bstr, or NULL on parser / allocation failure.
Ownership: the returned buffer is owned by the caller and should be released with fio_bstr_free. If bstr_target == NULL and rendering fails, the newly allocated output is freed. If bstr_target was provided, ownership stays with the caller.
#Rendering Behavior
From the implementation:
- normal text, code, and attribute values are escaped
- raw Markdown HTML blocks / inline HTML are preserved, with GFM tagfilter applied
- blocked tag names include
iframe,noembed,noframes,plaintext,style,textarea,title, andxmp; HTML blocks only filtertextareaandxmp - fenced code info strings use the first word as
class="language-..." - GFM tables render
<table><thead>...and open<tbody>for body rows - table cell alignment uses
align="left",align="right", oralign="center" - GFM task list items render disabled checkbox inputs
- autolinks add
mailto:for bare email addresses andhttp://forwww.links - image alt text is escaped and strips nested link / emphasis markup
#Thread-Safety
The renderer uses caller-provided input and a local renderer state. It is thread-safe as long as separate calls do not mutate the same bstr_target at the same time.
#Example
#define FIO_MD2HTML
#include "fio-stl.h"
#include <stdio.h>
int main(void) {
char md[] = "# Demo\n\nHello **world**.\n";
char *html = fio_md2html(NULL, FIO_BUF_INFO1(md));
if (!html)
return 1;
fwrite(html, 1, fio_bstr_len(html), stdout);
fio_bstr_free(html);
return 0;
}