]> git.frustrated-labs.net Git - frustrated-functor.dev.git/commitdiff
feat: add link and guid to generated rss entries
authorAlexander Goussas <[email protected]>
Sun, 3 May 2026 20:42:08 +0000 (15:42 -0500)
committerAlexander Goussas <[email protected]>
Sun, 3 May 2026 20:42:08 +0000 (15:42 -0500)
bin/blog-processor/src/rss_formatter.zig

index a6fabfc18f8c24ffca6d0621d73b095d25bda41d..8ad27f73eebc8efb4e3d95f950739ff876c5fa11 100644 (file)
@@ -5,12 +5,15 @@ const md = @import("./markdown_parser.zig");
 pub const RssFormatter = struct {
     buffer: std.ArrayList(u8),
     alloc: std.mem.Allocator,
+    /// URL of the live post
+    url: []const u8,
 
     /// Create a new RssFormatter that will use the provided allocator.
-    pub fn init(alloc: std.mem.Allocator) @This() {
+    pub fn init(alloc: std.mem.Allocator, url: []const u8) @This() {
         return .{
             .buffer = .empty,
             .alloc = alloc, 
+            .url = url,
         };
     }
 
@@ -24,29 +27,27 @@ pub const RssFormatter = struct {
             self.alloc,
             \\<item>
             \\<author>[email protected] (Alexander Goussas)</author>
-            \\<pubDate>
+            \\
         );
 
-        try self.buffer.appendSlice(self.alloc, doc.date);
-        try self.buffer.appendSlice(
-            self.alloc,
-            \\</pubDate>
-            \\<title>
-        );
+        try self.tag("<pubDate>", "</pubDate>", doc.date);
+        try self.tag("<title>", "</title>", doc.title);
+        try self.tag("<guid>", "</guid>", self.url);
+        try self.tag("<link>", "</link>", self.url);
 
-        try self.buffer.appendSlice(self.alloc, doc.title);
-        try self.buffer.appendSlice(
-            self.alloc,
-            \\</title>
-            \\</item>
-        );
-
-        // TODO: Add link
-        // TODO: Add guid (same as link)
+        try self.buffer.appendSlice(self.alloc, "</item>");
+            
         // TODO: Add a description
         
         return self.buffer.items;
     }
+
+    fn tag(self: *@This(), openingTag: []const u8, closingTag: []const u8, inside: []const u8) !void {
+        try self.buffer.appendSlice(self.alloc, openingTag);
+        try self.buffer.appendSlice(self.alloc, inside);
+        try self.buffer.appendSlice(self.alloc, closingTag);
+        try self.buffer.append(self.alloc, '\n');
+    }
 };
 
 test "rss formatter can format markdown document" {
@@ -60,7 +61,8 @@ test "rss formatter can format markdown document" {
         \\Aujourd'hui, mamam est morte.
         ;
 
-    var formatter = RssFormatter.init(alloc);
+    const postURL = "https://frustrated-functor.dev/a-post-title.md.html";
+    var formatter = RssFormatter.init(alloc, postURL);
     defer formatter.deinit();
 
     var mdDoc = try md.MarkdownDoc.parse(doc, alloc);
@@ -73,6 +75,8 @@ test "rss formatter can format markdown document" {
         \\<author>[email protected] (Alexander Goussas)</author>
         \\<pubDate>12 april 2026</pubDate>
         \\<title>A post title</title>
+        \\<guid>https://frustrated-functor.dev/a-post-title.md.html</guid>
+        \\<link>https://frustrated-functor.dev/a-post-title.md.html</link>
         \\</item>
         ,
         result