mirror of
https://github.com/nix-community/nixvim.git
synced 2025-11-08 19:46:06 +01:00
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).
43 lines
1.1 KiB
Nix
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)
|