From 59e2e422afc894e1b1c161815a6caccc1c19ad77 Mon Sep 17 00:00:00 2001
From: Valentin Gagarin <valentin@gagarin.work>
Date: Sat, 9 Nov 2024 01:52:20 +0100
Subject: [PATCH] make template overrides take final and prev

---
 content/default.nix   | 10 ++--------
 lib.nix               |  6 +++++-
 structure/article.nix | 44 +++++++++++++++++++++----------------------
 3 files changed, 28 insertions(+), 32 deletions(-)

diff --git a/content/default.nix b/content/default.nix
index 649b5d38..67b0040b 100644
--- a/content/default.nix
+++ b/content/default.nix
@@ -9,14 +9,12 @@ in
   collections.news.type = cfg.content-types.article;
 
   pages.index = { config, link, ... }: {
-    title = "Fediversity";
+    title = "Welcome to the Fediversity project";
     description = "Fediversity web site";
     summary = ''
       This web site hosts up-to-date information about the the NGI Zero Fediversity project.
     '';
     body = ''
-      # Welcome to the Fediversity project
-
       ${pages.fediversity.summary}
 
       [Learn more about Fediversity](${link pages.fediversity})
@@ -61,11 +59,7 @@ in
       }
     '';
     outputs.html = (cfg.templates.html.page config).override {
-      html.body.content = lib.mkForce [
-        # don't show the page title as a heading
-        (cfg.menus.main.outputs.html config)
-        (cfg.templates.html.markdown { inherit (config) name body; })
-      ];
+      html.head.title.text = "Fediversity";
     };
   };
 }
diff --git a/lib.nix b/lib.nix
index 47961906..7ada71f8 100644
--- a/lib.nix
+++ b/lib.nix
@@ -8,7 +8,11 @@ rec {
     result // {
       override = new:
         let
-          base' = lib.recursiveUpdate base new;
+          base' =
+            if lib.isFunction new
+            then lib.recursiveUpdate base (new base' base)
+            else
+              lib.recursiveUpdate base new;
           result' = g base';
         in
         result' // {
diff --git a/structure/article.nix b/structure/article.nix
index bf07ebc8..ac876113 100644
--- a/structure/article.nix
+++ b/structure/article.nix
@@ -27,28 +27,26 @@ in
       };
     };
     config.name = lib.slug config.title;
-    config.outputs.html = lib.mkForce ((cfg.templates.html.page config).override {
-      html = {
-        # TODO: make authors always a list
-        head.meta.authors = if lib.isList config.author then config.author else [ config.author ];
-        body.content = lib.mkForce [
-          (cfg.menus.main.outputs.html config)
-          {
-            section = {
-              heading = {
-                # TODO: i18n support
-                # TODO: structured dates
-                before = [{ p.content = "Published ${config.date}"; }];
-                content = config.title;
-                after = [{ p.content = "Written by ${config.author}"; }];
-              };
-              content = [
-                (cfg.templates.html.markdown { inherit (config) name body; })
-              ];
-            };
-          }
-        ];
-      };
-    });
+    config.outputs.html = lib.mkForce
+      ((cfg.templates.html.page config).override (final: prev: {
+        html = {
+          # TODO: make authors always a list
+          head.meta.authors = if lib.isList config.author then config.author else [ config.author ];
+          body.content = with lib; map
+            (e:
+              if isAttrs e && e ? section
+              then
+                recursiveUpdate e
+                  {
+                    section.heading = {
+                      before = [{ p.content = "Published ${config.date}"; }];
+                      after = [{ p.content = "Written by ${config.author}"; }];
+                    };
+                  }
+              else e
+            )
+            prev.html.body.content;
+        };
+      }));
   };
 }