|
| 1 | +{ |
| 2 | + lib, |
| 3 | + name, |
| 4 | + config, |
| 5 | + options, |
| 6 | + ... |
| 7 | +}: |
| 8 | +let |
| 9 | + cfg = config._category; |
| 10 | + |
| 11 | + pageType = lib.types.submoduleWith { |
| 12 | + modules = [ ./page.nix ]; |
| 13 | + }; |
| 14 | + |
| 15 | + pages = builtins.removeAttrs config (builtins.attrNames options); |
| 16 | +in |
| 17 | +{ |
| 18 | + freeformType = lib.types.attrsOf pageType; |
| 19 | + |
| 20 | + options._category = { |
| 21 | + name = lib.mkOption { |
| 22 | + type = lib.types.str; |
| 23 | + default = name; |
| 24 | + defaultText = lib.literalMD "attribute name"; |
| 25 | + }; |
| 26 | + |
| 27 | + order = lib.mkOption { |
| 28 | + type = lib.types.int; |
| 29 | + default = 100; |
| 30 | + description = "Priority for where this category will appear in the menu."; |
| 31 | + }; |
| 32 | + |
| 33 | + type = lib.mkOption { |
| 34 | + type = lib.types.enum [ |
| 35 | + "prefix" |
| 36 | + "normal" |
| 37 | + "suffix" |
| 38 | + ]; |
| 39 | + default = "normal"; |
| 40 | + description = '' |
| 41 | + The kind of mdbook chapters this category contains. |
| 42 | +
|
| 43 | + **Prefix Chapter** |
| 44 | + : Before the main numbered chapters, prefix chapters can be added that |
| 45 | + will not be numbered. This is useful for forewords, introductions, etc. |
| 46 | + There are, however, some constraints. |
| 47 | + Prefix chapters cannot be nested; they should all be on the root level. |
| 48 | + And you cannot add prefix chapters once you have added numbered chapters. |
| 49 | +
|
| 50 | + **Normal Chapter** |
| 51 | + : Called a "Numbered Chapter" in the MDBook docs. |
| 52 | + Numbered chapters outline the main content of the book and can be |
| 53 | + nested, resulting in a nice hierarchy (chapters, sub-chapters, etc.). |
| 54 | +
|
| 55 | + **Suffix Chapter** |
| 56 | + : Like prefix chapters, suffix chapters are unnumbered, but they come |
| 57 | + after numbered chapters. |
| 58 | +
|
| 59 | + See <https://rust-lang.github.io/mdBook/format/summary.html>. |
| 60 | + ''; |
| 61 | + }; |
| 62 | + |
| 63 | + text = lib.mkOption { |
| 64 | + type = lib.types.str; |
| 65 | + description = "The rendered menu."; |
| 66 | + readOnly = true; |
| 67 | + }; |
| 68 | + }; |
| 69 | + |
| 70 | + config._category = { |
| 71 | + text = lib.optionalString (pages != { }) '' |
| 72 | + # ${cfg.name} |
| 73 | +
|
| 74 | + ${lib.pipe pages [ |
| 75 | + builtins.attrValues |
| 76 | + (map ( |
| 77 | + page: |
| 78 | + page._page.toMenu { |
| 79 | + nested = cfg.type == "normal"; |
| 80 | + indent = ""; |
| 81 | + prefix = [ ]; |
| 82 | + inherit page; |
| 83 | + } |
| 84 | + )) |
| 85 | + (builtins.concatStringsSep "\n") |
| 86 | + ]} |
| 87 | + ''; |
| 88 | + }; |
| 89 | +} |
0 commit comments