diff options
| author | t <t@tjp.lol> | 2026-11-24 22:50:09 -0700 |
|---|---|---|
| committer | t <t@tjp.lol> | 2026-11-24 23:31:03 -0700 |
| commit | 2f1d226f6baaafec34b5b3623ba6c9df9aaa003b (patch) | |
| tree | 4dad14862384c296aaf7a2b6ac69dab0055486cf /src/doc.zig | |
| parent | 17e3a3589d8482f545bb5956a4f7ea01c646698f (diff) | |
Render Markdown in the editor and add a terminal launcher
Show links, images, quotes, rules and YAML frontmatter as formatted
text, revealing the source while the caret is on their line. Images
load only from local files and are never fetched.
Store frontmatter ahead of the hidden comment block so the file's
first line stays a YAML opener. Files saved the old way still open.
Add bin/write_away to launch or focus the app from a shell.
Diffstat (limited to 'src/doc.zig')
| -rw-r--r-- | src/doc.zig | 84 |
1 files changed, 77 insertions, 7 deletions
diff --git a/src/doc.zig b/src/doc.zig index 3860809..2320a6a 100644 --- a/src/doc.zig +++ b/src/doc.zig @@ -1,6 +1,7 @@ //! The document model and the file format. Pure Zig, no AppKit: `zig build test` covers it. const std = @import("std"); const ghost_io = @import("ghost_io.zig"); +const markdown = @import("markdown.zig"); const mem = std.mem; const Allocator = mem.Allocator; @@ -161,8 +162,10 @@ pub fn suggestion(line: []const u8) ?[]const u8 { // // v2 puts optional Overflow and write_away comments before the visible Markdown. One LF // separates the comments when both are present; two LFs separate the last comment from -// the exact visible bytes. Legacy comments were trailers. Offsets always address the full -// editor body, so v2 ghosts must be restored before the alternatives are reanchored. +// the exact visible bytes. YAML frontmatter is those bytes' one exception: it stays on the +// file's first line and the comments follow it. Legacy comments were trailers. Offsets +// always address the full editor body, so v2 ghosts must be restored before the +// alternatives are reanchored. const overflow_mark = "<!-- overflow -->"; // legacy const overflow_open = "<!-- overflow\n("; @@ -195,8 +198,13 @@ pub fn serialize(gpa: Allocator, f: File) ![]u8 { const visible = if (f.ghosts.len != 0) try ghost_io.visible(gpa, f.body, f.ghosts) else f.body; defer if (f.ghosts.len != 0) gpa.free(visible); + // Frontmatter cut off by the end of the file has no line break to put a comment after. + var front = frontmatter(visible); + if (front != 0 and visible[front - 1] != '\n') front = 0; + var out: std.ArrayList(u8) = .empty; errdefer out.deinit(gpa); + try out.appendSlice(gpa, visible[0..front]); if (f.overflow.len > 0) { try out.appendSlice(gpa, overflow_open); // Escape ampersands first conceptually: each input byte is emitted only once. @@ -225,10 +233,24 @@ pub fn serialize(gpa: Allocator, f: File) ![]u8 { try out.appendSlice(gpa, meta_close); } try out.appendSlice(gpa, framing); - try out.appendSlice(gpa, visible); + try out.appendSlice(gpa, visible[front..]); return out.toOwnedSlice(gpa); } +/// The length of the YAML frontmatter `bytes` opens with, through the line that closes it, +/// or 0. Without that line there is none: a first line of "---" is then only a rule. +pub fn frontmatter(bytes: []const u8) usize { + var at: usize = 0; + while (at < bytes.len) { + const stop = if (mem.findScalarPos(u8, bytes, at, '\n')) |nl| nl + 1 else bytes.len; + const delimiter = markdown.frontDelimiter(u8, bytes[at..stop], at == 0); + if (at != 0 and delimiter) return stop; + if (at == 0 and !delimiter) return 0; + at = stop; + } + return 0; +} + fn digest(bytes: []const u8) [Sha256.digest_length * 2]u8 { var hash: [Sha256.digest_length]u8 = undefined; Sha256.hash(bytes, &hash, .{}); @@ -239,12 +261,15 @@ fn digest(bytes: []const u8) [Sha256.digest_length * 2]u8 { /// interpret, keep the entire source in the editor, including the recovery metadata. pub fn parse(arena: Allocator, bytes: []const u8) !File { const unchanged: File = .{ .body = bytes }; - var rest = bytes; + // The comments lead the file, or follow the frontmatter that does. + var rest = bytes[frontmatter(bytes)..]; + if (!mem.startsWith(u8, rest, overflow_open) and !mem.startsWith(u8, rest, meta_open)) rest = bytes; + const head = bytes[0 .. bytes.len - rest.len]; var overflow: ?[]const u8 = null; if (mem.startsWith(u8, rest, overflow_open)) { const block = splitComment(rest, overflow_open, overflow_close) orelse return unchanged; if (mem.startsWith(u8, block.rest, framing)) return .{ - .body = block.rest[framing.len..], + .body = try mem.concat(arena, u8, &.{ head, block.rest[framing.len..] }), .overflow = try decodeOverflow(arena, block.payload), }; if (!mem.startsWith(u8, block.rest, "\n" ++ meta_open)) return parseLegacy(arena, bytes); @@ -258,7 +283,7 @@ pub fn parse(arena: Allocator, bytes: []const u8) !File { if (meta.v != 2 or !mem.startsWith(u8, block.rest, framing)) return unchanged; if ((meta.ghosts.len != 0) != (meta.visible_sha256 != null)) return unchanged; - const visible = block.rest[framing.len..]; + const visible = try mem.concat(arena, u8, &.{ head, block.rest[framing.len..] }); const body = if (meta.ghosts.len == 0) visible else body: { const expected = meta.visible_sha256 orelse return unchanged; if (!mem.eql(u8, &expected, &digest(visible))) return unchanged; @@ -430,7 +455,7 @@ pub fn classify(para: []const u8, start: usize, end: usize) Scope { const after = mem.trim(u8, para[end..], space); if (sel.len == 0) return .word; - const at_head = mem.trimStart(u8, before, "# ").len == 0; // heading marks do not count + const at_head = mem.trimStart(u8, before, "#> ").len == 0; // heading and quote marks do not count const starts = at_head or isStop(before[before.len - 1]); const ends = after.len == 0 or isStop(sel[sel.len - 1]); if (!starts or !ends) return .word; @@ -701,6 +726,51 @@ test "v2 restores adjacent UTF-8 ghosts and overlapping groups after an editor e try t.expectError(error.InvalidGhost, serialize(a, .{ .body = body, .ghosts = &.{.{ .at = 0, .text = last }} })); } +test "comments follow YAML frontmatter, which keeps the first line" { + const t = std.testing; + var state: std.heap.ArenaAllocator = .init(t.allocator); + defer state.deinit(); + const a = state.allocator(); + try t.expectEqual(7, frontmatter("---\n---")); + try t.expectEqual(17, frontmatter("--- \r\nk: v\n... \r\n---\n")); + for ([_][]const u8{ "", "---", "---\nk: v\n", "---\nk: v\n ---\n", "\n---\n---\n", "...\n---\n", "----\n---\n" }) |none| try t.expectEqual(0, frontmatter(none)); + + const front = "---\ntitle: café\n---\n"; + const rest = "\n# Title\n\n---\n"; + const file: File = .{ + .body = front ++ rest, + .overflow = "Kept.", + .groups = &.{.{ .at = front.len + 3, .variants = &.{ "Title", "Name" } }}, + .ghosts = &.{.{ .at = 4, .text = "title" }}, + }; + const bytes = try serialize(a, file); + try t.expect(mem.startsWith(u8, bytes, "---\n: café\n---\n" ++ overflow_open)); + try t.expect(mem.endsWith(u8, bytes, meta_close ++ framing ++ rest)); + const back = try parse(a, bytes); + try t.expectEqualStrings(file.body, back.body); + try t.expectEqualStrings("Kept.", back.overflow); + try t.expectEqual(front.len + 3, back.groups[0].at); + try t.expectEqual(4, back.ghosts[0].at); + const stashed = try serialize(a, .{ .body = front ++ rest, .overflow = "Kept." }); + try t.expectEqualStrings(front ++ overflow_open ++ "Kept." ++ overflow_close ++ framing ++ rest, stashed); + try t.expectEqualStrings(front ++ rest, (try parse(a, stashed)).body); + + // A file saved with its comments first still opens, and saves the new way. + const old = try parse(a, overflow_open ++ "Kept." ++ overflow_close ++ framing ++ front ++ rest); + try t.expectEqualStrings(front ++ rest, old.body); + try t.expectEqualStrings(stashed, try serialize(a, old)); + // With no line of its own to close on, or a ghost for a delimiter, the comments lead. + const cut_off = try serialize(a, .{ .body = "---\nk: v\n---", .overflow = "Kept." }); + try t.expect(mem.startsWith(u8, cut_off, overflow_open)); + try t.expectEqualStrings("---\nk: v\n---", (try parse(a, cut_off)).body); + const hidden: File = .{ .body = front ++ rest, .ghosts = &.{.{ .at = 0, .text = "---" }} }; + const leading = try serialize(a, hidden); + try t.expect(mem.startsWith(u8, leading, meta_open)); + try t.expectEqualStrings(hidden.body, (try parse(a, leading)).body); + // Frontmatter before a comment that cannot be read is left exactly as it is. + try expectRaw(a, front ++ meta_open ++ "{nope" ++ meta_close ++ framing ++ rest); +} + test "plain Markdown is byte-exact; v1 ghosts and ghost-free v2 groups still follow their text" { const t = std.testing; var state: std.heap.ArenaAllocator = .init(t.allocator); |
