1
0
Fork 0
mirror of https://github.com/nix-community/nixvim.git synced 2025-11-08 19:46:06 +01:00
nixvim/docs/modules/to-menu.nix
Matt Sturgeon 4f03ca05d9 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).
2025-09-30 16:21:23 +00:00

43 lines
1.1 KiB
Nix

{
lib,
optionNames,
}:
/**
The default `toMenu` function renders a page node into a menu subtree.
*/
{
page,
prefix ? [ ],
indent ? "",
nested ? true,
}:
let
inherit (page._page) loc target;
count = page._page.children;
# Only add node to the menu if it has content or multiple children
showInMenu = target != "" || count > 1;
nextPrefix = if showInMenu then loc else prefix;
nextIndent = if showInMenu && nested then indent + " " else indent;
children = builtins.removeAttrs page optionNames;
submenu = lib.pipe children [
builtins.attrValues
(map (
subpage:
page._page.toMenu {
inherit nested;
page = subpage;
indent = nextIndent;
prefix = nextPrefix;
}
))
];
loc' = if lib.lists.hasPrefix prefix loc then lib.lists.drop (builtins.length prefix) loc else loc;
menuText = lib.attrsets.showAttrPath loc';
menuitem = lib.optionals showInMenu [
(indent + lib.optionalString nested "- " + "[${menuText}](${target})")
];
in
builtins.concatStringsSep "\n" (menuitem ++ submenu)