Skip to content

jetzig-framework/zmd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zmd

Zmd is a Markdown parser and HTML translator written in 100% pure Zig with zero dependencies.

Zmd is used by the Jetzig web framework.

Supported syntax

  • Headers (H1->H6)
  • bold
  • italic
  • code
  • Links
  • Images
  • Fenced code blocks (with info string)
  • Ordered lists
  • Unordered lists

Usage

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);
}

Customization

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 on block elements. This is the language specifier (zig) in this example:
if (true) std.debug.print("some zig code");
  • href, title - provided on image and link 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.

License

Zmd is MIT-licensed.

About

Zmd is a Markdown library written in Zig

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages