feat(content): parse document preemble
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Has been cancelled

Show title stored in preemble of markdown file if available.
This commit is contained in:
2025-11-01 17:41:33 +01:00
parent 74bf941820
commit 55861114dc
4 changed files with 73 additions and 22 deletions

View File

@@ -52,8 +52,14 @@ pub fn Content(App: type) type {
fn handle(ctx: *anyopaque, model: *App.Model, event: App.Event) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
switch (event) {
.about => model.page = .about,
.blog => model.page = .blog,
.about => {
model.page = .about;
model.document = .init(@embedFile("about"));
},
.blog => {
model.page = .blog;
model.document = .init(@embedFile("blog"));
},
else => {},
}
this.page = model.page;
@@ -63,10 +69,7 @@ pub fn Content(App: type) type {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
const text = switch (model.page) {
.about => @embedFile("about"),
.blog => @embedFile("blog"),
};
const text = model.document.content;
var index: usize = 0;
for (0..size.y) |row| {
@@ -89,12 +92,8 @@ pub fn Content(App: type) type {
pub fn Title(App: type) type {
return struct {
text: []const u8,
pub fn init() @This() {
return .{
.text = "<Title>",
};
return .{};
}
pub fn element(this: *@This()) App.Element {
@@ -106,16 +105,20 @@ pub fn Title(App: type) type {
};
}
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
const this: *const @This() = @ptrCast(@alignCast(ctx));
_ = this;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
for (0.., this.text) |idx, cp| {
cells[idx].style.fg = .green;
cells[idx].style.emphasis = &.{.bold};
cells[idx].cp = cp;
// NOTE do not write over the contents of this `Container`'s `Size`
if (idx == cells.len - 1) break;
if (model.document.title) |text| {
for (0.., text) |idx, cp| {
cells[idx].style.fg = .green;
cells[idx].style.emphasis = &.{.bold};
cells[idx].cp = cp;
// NOTE do not write over the contents of this `Container`'s `Size`
if (idx == cells.len - 1) break;
}
}
}
};