add(zmd): parse markdown file contents and display the parsed contents
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 44s

Not everything can be parsed and displayed correctly yet, however this
will the default file format to use for adding page contents (i.e.
normal pages, blog entries, etc.)
This commit is contained in:
2024-10-13 12:57:44 +02:00
parent 2db1d55fcf
commit a01619911d
6 changed files with 184 additions and 50 deletions

117
src/widget/node2buffer.zig Normal file
View File

@@ -0,0 +1,117 @@
///! Transform a given `zmd.Note` into a buffer which can be used by any `vaxis.widgets.View`
const std = @import("std");
const vaxis = @import("vaxis");
const zmd = @import("zmd");
pub fn toBuffer(node: *zmd.Node, allocator: std.mem.Allocator, input: []const u8, array: *std.ArrayList(vaxis.Cell), s: vaxis.Cell.Style) !void {
const content = switch (node.token.element.type) {
.text => input[node.token.start..node.token.end],
.code, .block => node.content,
else => "",
};
var style = s;
// determine general styling changes
switch (node.token.element.type) {
.bold => {
style.bold = true;
},
.bold_close => {
style.bold = false;
},
.italic => {
style.italic = true;
},
.italic_close => {
style.italic = false;
},
.block => {
// TODO: what should I do with blocks?
},
.block_close => {
// TODO: what should I do with blocks?
},
.code => {
style.dim = true;
},
.code_close => {
style.dim = false;
},
else => {},
}
switch (node.token.element.type) {
.root, .none, .eof => {},
.linebreak, .paragraph => {
try array.append(.{
.char = .{ .grapheme = "\n" },
.style = style,
});
style.ul_style = .off;
},
.h1 => {
style.ul_style = .single;
try array.append(.{
.char = .{ .grapheme = "#" },
.style = style,
});
},
.h2 => {
style.ul_style = .single;
for (0..2) |_| {
try array.append(.{
.char = .{ .grapheme = "#" },
.style = style,
});
}
},
.h3 => {
style.ul_style = .single;
for (0..3) |_| {
try array.append(.{
.char = .{ .grapheme = "#" },
.style = style,
});
}
},
.h4 => {
style.ul_style = .single;
for (0..4) |_| {
try array.append(.{
.char = .{ .grapheme = "#" },
.style = style,
});
}
},
.h5 => {
style.ul_style = .single;
for (0..5) |_| {
try array.append(.{
.char = .{ .grapheme = "#" },
.style = style,
});
}
},
.h6 => {
style.ul_style = .single;
for (0..6) |_| {
try array.append(.{
.char = .{ .grapheme = "#" },
.style = style,
});
}
},
else => {
for (content, 0..) |_, i| {
try array.append(.{
.char = .{ .grapheme = content[i .. i + 1] },
.style = style,
});
}
},
}
for (node.children.items) |child_node| {
try toBuffer(child_node, allocator, input, array, style);
}
}