implement navigation

This commit is contained in:
Valentin Gagarin 2024-11-13 15:24:41 +01:00 committed by Valentin Gagarin
parent 3a36a6cf29
commit ad8b758cee
5 changed files with 93 additions and 4 deletions

View file

@ -0,0 +1,21 @@
{ config, ... }:
let
inherit (config) pages;
in
{
menus.main = {
label = "Main";
items = [
{
menu.label = "Consortium";
menu.items = map (page: { inherit page; }) (with pages; [ nlnet oid tweag nordunet ]);
}
{
page = pages.fediversity;
}
{
page = pages.grants;
}
];
};
}

View file

@ -37,7 +37,10 @@ in
<meta name="description" content="${page.description}" /> <meta name="description" content="${page.description}" />
<link rel="canonical" href="${page.outPath}" /> <link rel="canonical" href="${page.outPath}" />
''; '';
body = builtins.readFile (commonmark page.name page.body); body = ''
${templates.nav { menu = { menu = config.menus.main; }; }}
${builtins.readFile (commonmark page.name page.body)}
'';
}); });
}); });
article = lib.mkDefault (config: page: { article = lib.mkDefault (config: page: {
@ -48,7 +51,10 @@ in
<meta name="description" content="${page.description}" /> <meta name="description" content="${page.description}" />
<meta name="author" content="${with lib; if isList page.author then join ", " page.author else page.author}" /> <meta name="author" content="${with lib; if isList page.author then join ", " page.author else page.author}" />
''; '';
body = builtins.readFile (commonmark page.name page.body); body = ''
${templates.nav { menu = { menu = config.menus.main; }; }}
${builtins.readFile (commonmark page.name page.body)}
'';
}); });
}); });
}; };

View file

@ -1,5 +1,5 @@
{ lib }: { lib }:
{ rec {
html = { head, body }: '' html = { head, body }: ''
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
@ -14,4 +14,24 @@
<body> <body>
</html> </html>
''; '';
nav = { menu }:
let
render-item = item:
if item ? menu then
''
<li>${item.menu.label}
${lib.indent " " (nav { menu = item; })}
''
else
if item ? page then ''<li><a href="${item.page}">${item.page.title}</a></li>''
else ''<li><a href="${item.link.url}">${item.link.label}</a></li>''
;
in
''
<nav>
<ul>
${with lib; indent " " (join "\n" (map render-item menu.menu.items))}
</ul>
</nav>
'';
} }

View file

@ -17,7 +17,7 @@ in
page = { name, config, ... }: { page = { name, config, ... }: {
options = { options = {
name = mkOption { name = mkOption {
description = "Symbolic name for the page, used as a human-readable identifier"; description = "Symbolic name, used as a human-readable identifier";
type = types.str; type = types.str;
default = name; default = name;
}; };
@ -93,5 +93,41 @@ in
config.outPath = "${collectionName}/${lib.head config.locations}"; config.outPath = "${collectionName}/${lib.head config.locations}";
config.template = cfg.templates.article; config.template = cfg.templates.article;
}; };
named-link = { ... }: {
options = {
label = mkOption {
description = "Link label";
type = types.str;
};
url = mkOption {
description = "Link URL";
type = types.str;
};
};
};
navigation = { name, ... }: {
options = {
name = mkOption {
description = "Symbolic name, used as a human-readable identifier";
type = types.str;
default = name;
};
label = mkOption {
description = "Menu label";
type = types.str;
default = name;
};
items = mkOption {
description = "List of menu items";
type = with types; listOf (attrTag {
menu = mkOption { type = submodule cfg.content-types.navigation; };
page = mkOption { type = submodule cfg.content-types.page; };
link = mkOption { type = submodule cfg.content-types.named-link; };
});
};
};
};
}; };
} }

View file

@ -54,4 +54,10 @@ in
})); }));
}; };
options.menus = mkOption {
description = ''
Collection navigation menus
'';
type = with types; attrsOf (submodule config.content-types.navigation);
};
} }