let navigation have its own template

This commit is contained in:
Valentin Gagarin 2024-11-13 15:24:41 +01:00
parent e3578b0440
commit c5966cd4cb
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.
'';
# 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 = ''
<title>${page.title}</title>
@ -41,7 +45,7 @@ in
<link rel="canonical" href="${page.outPath}" />
'';
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)}
'';
});

View file

@ -14,17 +14,16 @@ rec {
<body>
</html>
'';
nav = { page, menu }:
nav = menu: page:
let
render-item = item:
if item ? menu then
''
<li>${item.menu.label}
${lib.indent " " (nav { inherit page; menu = item; })}
''
else
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>''
if item ? menu then ''
<li>${item.menu.label}
${lib.indent " " (item.menu.outputs.html page)}
</li>
''
else 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>''
;
in
''

View file

@ -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; };
};
};
};
}