From e2691f8469bbc3888bbaea1cb7cd1b5f4cb507da Mon Sep 17 00:00:00 2001 From: valentin gagarin Date: Wed, 13 Nov 2024 15:24:41 +0100 Subject: [PATCH] let navigation have its own template --- website/presentation/default.nix | 8 ++++++-- website/presentation/templates.nix | 17 ++++++++--------- website/structure/navigation.nix | 11 ++++++++++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/website/presentation/default.nix b/website/presentation/default.nix index 4448fcb..0d3eb28 100644 --- a/website/presentation/default.nix +++ b/website/presentation/default.nix @@ -21,6 +21,9 @@ in Each template function takes the complete site `config` and the document's data structure. ''; + # TODO: this function should probably take a single attrs, + # otherwise it's quite inflexible. + # named parameters would also help with readability at the call site type = recursiveAttrs (with types; functionTo (functionTo str)); }; @@ -34,6 +37,7 @@ in ''; in { + nav = lib.mkDefault templates.nav; page = lib.mkDefault (config: page: templates.html { head = '' ${page.title} @@ -41,7 +45,7 @@ in ''; body = '' - ${templates.nav { inherit page; menu = { menu = config.menus.main; }; }} + ${config.menus.main.outputs.html page} ${builtins.readFile (commonmark page.name page.body)} ''; }); @@ -55,7 +59,7 @@ in } ''; body = '' - ${templates.nav { inherit page; menu = { menu = config.menus.main; }; }} + ${config.menus.main.outputs.html page} ${builtins.readFile (commonmark page.name page.body)} ''; }); diff --git a/website/presentation/templates.nix b/website/presentation/templates.nix index f4567aa..d33630b 100644 --- a/website/presentation/templates.nix +++ b/website/presentation/templates.nix @@ -14,17 +14,16 @@ rec { ''; - nav = { page, menu }: + nav = menu: page: let render-item = item: - if item ? menu then - '' -
  • ${item.menu.label} - ${lib.indent " " (nav { inherit page; menu = item; })} - '' - else - if item ? page then ''
  • ${item.page.title}
  • '' - else ''
  • ${item.link.label}
  • '' + if item ? menu then '' +
  • ${item.menu.label} + ${lib.indent " " (item.menu.outputs.html page)} +
  • + '' + else if item ? page then ''
  • ${item.page.title}
  • '' + else ''
  • ${item.link.label}
  • '' ; in '' diff --git a/website/structure/navigation.nix b/website/structure/navigation.nix index 1267027..a3d7836 100644 --- a/website/structure/navigation.nix +++ b/website/structure/navigation.nix @@ -29,7 +29,7 @@ in }; }; - content-types.navigation = { name, ... }: { + content-types.navigation = { name, config, ... }: { options = { name = mkOption { description = "Symbolic name, used as a human-readable identifier"; @@ -49,6 +49,15 @@ in link = mkOption { type = submodule cfg.content-types.named-link; }; }); }; + outputs = mkOption { + description = '' + Representations of the navigation structure in different formats + + It must be a function that takes the page on which the navigation is to be shown, such that relative links get computed correctly. + ''; + type = with types; attrsOf (functionTo str); + default.html = cfg.templates.html.nav { menu = config; }; + }; }; }; }