let navigation have its own template

This commit is contained in:
Valentin Gagarin 2024-11-13 15:24:41 +01:00 committed by Valentin Gagarin
parent 84f5d17e3e
commit e2691f8469
3 changed files with 24 additions and 12 deletions

View file

@ -21,6 +21,9 @@ in
Each template function takes the complete site `config` and the document's data structure. 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)); type = recursiveAttrs (with types; functionTo (functionTo str));
}; };
@ -34,6 +37,7 @@ in
''; '';
in in
{ {
nav = lib.mkDefault templates.nav;
page = lib.mkDefault (config: page: templates.html { page = lib.mkDefault (config: page: templates.html {
head = '' head = ''
<title>${page.title}</title> <title>${page.title}</title>
@ -41,7 +45,7 @@ in
<link rel="canonical" href="${page.outPath}" /> <link rel="canonical" href="${page.outPath}" />
''; '';
body = '' body = ''
${templates.nav { inherit page; menu = { menu = config.menus.main; }; }} ${config.menus.main.outputs.html page}
${builtins.readFile (commonmark page.name page.body)} ${builtins.readFile (commonmark page.name page.body)}
''; '';
}); });
@ -55,7 +59,7 @@ in
} }
''; '';
body = '' body = ''
${templates.nav { inherit page; menu = { menu = config.menus.main; }; }} ${config.menus.main.outputs.html page}
${builtins.readFile (commonmark page.name page.body)} ${builtins.readFile (commonmark page.name page.body)}
''; '';
}); });

View file

@ -14,16 +14,15 @@ rec {
<body> <body>
</html> </html>
''; '';
nav = { page, menu }: nav = menu: page:
let let
render-item = item: render-item = item:
if item ? menu then if item ? menu then ''
''
<li>${item.menu.label} <li>${item.menu.label}
${lib.indent " " (nav { inherit page; menu = item; })} ${lib.indent " " (item.menu.outputs.html page)}
</li>
'' ''
else else if item ? page then ''<li><a href="${page.link item.page}">${item.page.title}</a></li>''
if item ? page then ''<li><a href="${page.link item.page}">${item.page.title}</a></li>''
else ''<li><a href="${item.link.url}">${item.link.label}</a></li>'' else ''<li><a href="${item.link.url}">${item.link.label}</a></li>''
; ;
in in

View file

@ -29,7 +29,7 @@ in
}; };
}; };
content-types.navigation = { name, ... }: { content-types.navigation = { name, config, ... }: {
options = { options = {
name = mkOption { name = mkOption {
description = "Symbolic name, used as a human-readable identifier"; description = "Symbolic name, used as a human-readable identifier";
@ -49,6 +49,15 @@ in
link = mkOption { type = submodule cfg.content-types.named-link; }; 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; };
};
}; };
}; };
} }