1
0
Fork 0
mirror of https://github.com/nix-community/nixvim.git synced 2025-12-14 13:01:12 +01:00

docs/lib: generalise menu impl using module system

Move the mdbook menu rendering code into the module system and
generalise it to apply to multiple "categories" (mdbook parts) and
"types" of category (prefix, suffix, etc).
This commit is contained in:
Matt Sturgeon 2025-09-23 18:25:39 +01:00
parent 2f952af4a7
commit 4f03ca05d9
8 changed files with 228 additions and 60 deletions

View file

@ -10,19 +10,14 @@
}:
let
pageConfiguration = lib.evalModules {
menuConfiguration = lib.evalModules {
modules = [
pageSpecs
{
freeformType = lib.types.attrsOf (
lib.types.submoduleWith {
modules = [ ../modules/page.nix ];
}
);
}
../modules/menu.nix
];
};
pages = pageConfiguration.config;
cfg = menuConfiguration.config;
pages = cfg.functions;
# Collect all page nodes into a list of page entries
collectPages =
@ -33,7 +28,7 @@ let
children = builtins.removeAttrs node [ "_page" ];
in
lib.optional (node ? _page) node._page ++ lib.optionals (children != { }) (collectPages children)
) (builtins.attrValues pages);
) (builtins.attrValues (builtins.removeAttrs pages [ "_category" ]));
# Normalised page specs
pageList = collectPages pages;
@ -60,11 +55,9 @@ let
}
);
passthru.config = pageConfiguration;
passthru.config = menuConfiguration;
passthru.menu = import ./menu.nix {
inherit lib pages;
};
passthru.menu = cfg._menu.text;
passthru.pages = map (page: "${result}/${page.target}") pagesToRender;
}

View file

@ -1,31 +0,0 @@
{
lib,
pages,
indentSize ? " ",
}:
let
pageToLines =
indent: parent: node:
let
children = lib.pipe node [
(lib.flip builtins.removeAttrs [ "_page" ])
builtins.attrValues
];
# Only add node to the menu if it has content or multiple children
useNodeInMenu = node._page.target != "" || node._page.children > 1;
nextParent = if useNodeInMenu then node else parent;
nextIndent = if useNodeInMenu then indent + indentSize else indent;
loc = lib.lists.removePrefix (parent._page.loc or [ ]) node._page.loc;
menuName = lib.attrsets.showAttrPath loc;
in
lib.optional useNodeInMenu "${indent}- [${menuName}](${node._page.target})"
++ lib.optionals (children != [ ]) (
builtins.concatMap (pageToLines nextIndent nextParent) children
);
in
lib.pipe pages [
builtins.attrValues
(builtins.concatMap (pageToLines "" null))
lib.concatLines
]

View file

@ -4,19 +4,23 @@
# If there is an issue parsing the file, the resulting markdown will not contain any function docs.
{
lib.nixvim = {
_page = {
title = "lib.nixvim: Nixvim's functions";
source = ./index.md;
};
functions = {
_category.name = "Functions";
utils._page = {
title = "lib.nixvim.utils: utility functions";
functions.file = ../../lib/utils.nix;
};
lua._page = {
title = "lib.nixvim.lua: lua functions";
functions.file = ../../lib/to-lua.nix;
lib.nixvim = {
_page = {
title = "lib.nixvim: Nixvim's functions";
source = ./index.md;
};
utils._page = {
title = "lib.nixvim.utils: utility functions";
functions.file = ../../lib/utils.nix;
};
lua._page = {
title = "lib.nixvim.lua: lua functions";
functions.file = ../../lib/to-lua.nix;
};
};
};
}