Zmd is a Markdown parser and HTML translator written in 100% pure Zig with zero dependencies.
Zmd is used by the Jetzig web framework.
- Headers (H1->H6)
- bold
- italic
code
- Links
- Images
- Fenced code blocks (with info string)
- Ordered lists
- Unordered lists
const std = @import("std");
const zmd = @import("zmd");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const markdown = "# Header";
const html = try zmd.parse(allocator, markdown, .{});
defer allocator.free(html);
const stdout = std.fs.File.stdout();
try stdout.writeAll(html);
}
Formatter for supported markdown elements can be overridden with fuctions:
const html = zmd.parse(alloc, markdown, .{
.block = myBlock,
})
The function signature for formatters is fn (Allocator, Node) ![]const u8
Some node types provie special attributes such as:
meta
- provided onblock
elements. This is the language specifier (zig
) in this example:
if (true) std.debug.print("some zig code");
href
,title
- provided onimage
andlink
elements.
fn myBlock(allocator: std.mem.Allocator, node: zmd.Node) ![]const u8 {
const style = "font-family: Monospace;";
return if (node.meta) |meta|
std.fmt.allocPrint(allocator,
\\<pre class="language-{s}" style="{s}"><code>{s}</code></pre>
, .{ meta, style, node.content })
else
std.fmt.allocPrint(allocator,
\\<pre style="{s}"><code>{s}</code></pre>
, .{ style, node.content });
}
See src/zmd/Handlers.zig for the full reference.
Zmd is MIT-licensed.