Skip to content

jetzig-framework/zmd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 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
  • template blocks ({{...}})

Usage

const std = @import("std");
const zmd = @import("zmd");

pub fn main(init: std.process.Init) !void {
    const markdown =
        \\# Header
        \\## Sub-header
        \\### Sub-sub-header
        \\
        \\some text in **bold** and _italic_
        \\
        \\a paragraph
        \\
        \\```
        \\some code
        \\some more code in the same block
        \\and yet more code
        \\```
        \\some more text with a `code` fragment
        \\- list item
        \\  1. ordered item
    ;

    const stdout: std.Io.File = .stdout();
    var buf: [256]u8 = undefined;
    var writer = stdout.writer(init.io, &buf);
    try zmd.parseSlice(markdown, &writer.interface, .{});
}

Customization

Formatter for supported markdown elements can be overridden with fuctions:

const html = zmd.parseAlloc(alloc, markdown, .{
    .block = myBlock,
})

The function signature for formatters is fn (*Writer, Node) !void

Node provides 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(writer: *Writer, node: zmd.Node) !void {
    // writer writes opening string
    try writer.writeAll(
        \\<pre style="font-family: Monospace;"
    );
    if (node.meta) |meta|
        try writer.print(
            \\ class="language-{s}"
        , .{meta});
    try writer.writeAll(
        \\>
        \\<code>
        \\
    );

    // content is parsed and written

    // closing string
    return
    \\
    \\</code>
    \\</pre>
    \\
    ;
}

See Config.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

 
 
 

Contributors

Languages