Skip to content

jetzig-framework/zmd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 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").Zmd;
const fragments = @import("zmd").html.DefaultFragments;

pub fn main() !void {
    var zmd = Zmd.init(std.testing.allocator);
    defer zmd.deinit();

    try zmd.parse(
        \\# Header
        \\## Sub-header
        \\### Sub-sub-header
        \\
        \\some text in **bold** and _italic_
        \\
        \\a paragraph
        \\
        \\```zig
        \\some code
        \\```
        \\some more text with a `code` fragment
    );

    const html = try zmd.toHtml(fragments);
    defer std.testing.allocator.free(html);
}

Customization

The default HTML formatter provides a set of fragments that can be overridden. Fragments can be either:

  • A two-element tuple containing an open and close tag (e.g. .{ "<div>", </div>" });
  • A function that receives std.mem.Allocator and zmd.Node, returning ![]const u8.

Simply define a struct with the appropriate declarations of either type and Zmd will use the provided fragments, falling back to defaults for anything that is not defined.

Some node types provie special attributes such as:

  • meta - provided on block elements. This is the language specifier (zig) in this example:
```zig
if (true) std.debug.print("some zig code");
```
  • href, title - provided on image and link elements.
const MyFragments = struct {
    pub const h1 = .{ "<h1 class='text-xl font-bold'>", "</h1>\n" };

    pub fn block(allocator: std.mem.Allocator, node: 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 });
    }
}

And then:

const html = try zmd.toHtml(MyFragments);

See src/zmd/html.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 2

  •  
  •  

Languages