Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: switch to mdbook #471

Merged
merged 12 commits into from
Aug 7, 2023
39 changes: 0 additions & 39 deletions docs.nix

This file was deleted.

11 changes: 11 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Structure for nixvim docs

# Options

@NIXVIM_OPTIONS@

#

---

[Contributing](./CONTRIBUTING.md)
10 changes: 10 additions & 0 deletions docs/book.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[book]
authors = []
language = "en"
multilingual = false
src = "."
title = "nixvim docs"

[output.html.fold]
enable = true
level = 0
235 changes: 235 additions & 0 deletions docs/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
{
pkgs,
lib,
modules,
...
}:
with lib; let
options = lib.evalModules {
inherit modules;
specialArgs = {inherit pkgs lib;};
};

mkMDDoc = options:
(pkgs.nixosOptionsDoc {
options = filterAttrs (k: _: k != "_module") options;
warningsAreErrors = false;
transformOptions = opts:
opts
// {
declarations = with builtins;
map
(
decl:
if hasPrefix "/nix/store/" decl
then let
filepath = toString (match "^/nix/store/[^/]*/(.*)" decl);
in {
url = "https://github.com/nix-community/nixvim/blob/main/${filepath}";
name = filepath;
}
else decl
)
opts.declarations;
};
})
.optionsCommonMark;

removeUnwanted = attrs:
builtins.removeAttrs attrs [
"_module"
"_freeformOptions"
"warnings"
"assertions"
"content"
];

removeWhitespace = builtins.replaceStrings [" "] [""];

getSubOptions = opts: path: removeUnwanted (opts.type.getSubOptions path);

wrapModule = path: opts: isOpt: rec {
index = {
options =
if isOpt
then opts
else filterAttrs (_: component: component.isOption) opts;
path = concatStringsSep "/" path;
};

components =
if isOpt
then {}
else filterAttrs (_: component: !component.isOption) opts;

hasComponents = components != {};

isOption = isOpt;
};

processModulesRec = modules: let
recurse = path: mods: let
g = name: opts:
if !isOption opts
then wrapModule (path ++ [name]) (recurse (path ++ [name]) opts) false
else let
subOpts = getSubOptions opts (path ++ [name]);
in
if subOpts != {}
then
wrapModule
(path ++ [name])
(
(recurse (path ++ [name]) subOpts)
// {
# This is necessary to include the submodule option's definition in the docs (description, type, etc.)
# For instance, this helps submodules like "autoCmd" to include their base definitions and examples in the docs
# Though there might be a better, less "hacky" solution than this.
${name} = recursiveUpdate opts {
isOption = true;
type.getSubOptions = _: _: {}; # Used to exclude suboptions from the submodule definition itself
};
}
)
false
else wrapModule (path ++ [name]) opts true;
in
mapAttrs g mods;
in
foldlAttrs
(
acc: name: opts: let
group =
if !opts.hasComponents
then "Neovim Options"
else "none";

last =
acc.${group}
or {
index = {
options = {};
path = removeWhitespace "${group}";
};
components = {};
isGroup = true;
hasComponents = false;
};

isOpt = !opts.hasComponents && (isOption opts.index.options);
in
acc
// {
${group} = recursiveUpdate last {
index.options = optionalAttrs isOpt {
${name} = opts.index.options;
};

components = optionalAttrs (!isOpt) {
${name} = recursiveUpdate opts {
index.path = removeWhitespace (concatStringsSep "/" ((optional (group != "none") group) ++ [opts.index.path]));
hasComponents = true;
};
};

hasComponents = last.components != {};
};
}
)
{}
(recurse [] modules);

mapModulesToString = f: modules: let
recurse = mods: let
g = name: opts:
if (opts ? "isGroup")
then
if name != "none"
then (f name opts) + ("\n" + recurse opts.components)
else recurse opts.components
else if opts.hasComponents
then (f name opts) + ("\n" + recurse opts.components)
else f name opts;
in
concatStringsSep "\n" (mapAttrsToList g mods);
in
recurse modules;

docs = rec {
modules = processModulesRec (removeUnwanted options.options);
commands =
mapModulesToString
(
name: opts: let
isBranch =
if (hasSuffix "index" opts.index.path)
then true
else opts.hasComponents;
path =
if isBranch
then "${opts.index.path}/index.md"
else "${opts.index.path}.md";
in
(optionalString isBranch
"mkdir -p ${opts.index.path}\n")
+ "cp ${mkMDDoc opts.index.options} ${path}"
)
modules;
};

mdbook = {
nixvimOptions =
mapModulesToString
(
name: opts: let
isBranch =
if name == "index"
then true
else opts.hasComponents && opts.index.options != {};

path =
if isBranch
then "${opts.index.path}/index.md"
else if !opts.hasComponents
then "${opts.index.path}.md"
else "";

indentLevel = with builtins; length (filter isString (split "/" opts.index.path)) - 1;

padding = concatStrings (builtins.genList (_: "\t") indentLevel);
in "${padding}- [${name}](${path})"
)
docs.modules;
};

prepareMD = ''
# Copy inputs into the build directory
cp -r --no-preserve=all $inputs/* ./
cp ${../CONTRIBUTING.md} ./CONTRIBUTING.md

# Copy the generated MD docs into the build directory
# Using pkgs.writeShellScript helps to avoid the "bash: argument list too long" error
${pkgs.writeShellScript "copy_docs" docs.commands}

# Prepare SUMMARY.md for mdBook
# Using pkgs.writeText helps to avoid the same error as above
substituteInPlace ./SUMMARY.md \
--replace "@NIXVIM_OPTIONS@" "$(cat ${pkgs.writeText "nixvim-options-summary.md" mdbook.nixvimOptions})"
'';
in
pkgs.stdenv.mkDerivation {
name = "nixvim-docs";

phases = ["buildPhase"];

buildInputs = [pkgs.mdbook];
inputs = sourceFilesBySuffices ./. [".md" ".toml" ".js"];

buildPhase = ''
dest=$out/share/doc
mkdir -p $dest
${prepareMD}
mdbook build
cp -r ./book/* $dest
'';
}
6 changes: 6 additions & 0 deletions docs/highlight.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
};
};
packages = {
docs = pkgs-unfree.callPackage (import ./docs.nix) {
docs = pkgs-unfree.callPackage (import ./docs) {
modules = nixvimModules;
};
runUpdates =
Expand Down
4 changes: 2 additions & 2 deletions plugins/colorschemes/kanagawa.nix
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ in {
Change specific usages for a certain theme, or for all of them

Example:
```
```nix
{
wave = {
ui = {
Expand All @@ -103,7 +103,7 @@ in {
Change all usages of these colors.

Example:
```
```nix
{
sumiInk0 = "#000000";
fujiWhite = "#FFFFFF";
Expand Down
6 changes: 3 additions & 3 deletions plugins/completion/copilot-lua.nix
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ in {
Each value can be either a boolean or a lua function that returns a boolean.

Example:
```
```nix
{
markdown = true; # overrides default
terraform = false; # disallow specific filetype
Expand All @@ -107,7 +107,7 @@ in {

The key `"*"` can be used to disable the default configuration.
Example:
```
```nix
{
javascript = true; # allow specific filetype
typescript = true; # allow specific filetype
Expand Down Expand Up @@ -138,7 +138,7 @@ in {
See `:h vim.lsp.start_client` for list of options.

Example:
```
```nix
{
trace = "verbose";
settings = {
Expand Down
2 changes: 1 addition & 1 deletion plugins/completion/nvim-cmp/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ in {
- ultisnips

You can also provide a custom function:
```
```nix
{
__raw = \'\'
function(args)
Expand Down
2 changes: 1 addition & 1 deletion plugins/dap/dap-ui.nix
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ in {
layouts =
helpers.defaultNullOpts.mkNullable (types.listOf layoutOption)
''
```
```nix
[
{
elements = [
Expand Down
Loading