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

feat: Minimal format #140

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ The following is an example of markdown documentation for new and current users

> Note: Within nixpkgs the convention of using [definition-lists](https://www.markdownguide.org/extended-syntax/#definition-lists) for documenting arguments has been established.

## Usage

Refer to `nixdoc --help` for the most up-to-date usage information.

For a minimal format, suitable for inclusion into a dedicated documentation page, use:

```sh
nixdoc --file lib.nix --category "" --description "" --prefix "" --anchor-prefix "" >lib.md
```

## Custom nixdoc format (Legacy)

Expand Down
16 changes: 11 additions & 5 deletions src/commonmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,17 @@ impl ManualEntry {
}

/// Write a single CommonMark entry for a documented Nix function.
pub fn write_section(self, output: &mut String) -> String {
///
/// # Arguments
///
/// - `anchor_prefix`: The prefix to use for the anchor links.
/// In Nixpkgs this would be "function-library-".
/// - `output`: The output string to append the CommonMark onto.
pub fn write_section(self, anchor_prefix: &str, output: &mut String) -> String {
let (ident, title) = self.get_ident_title();
output.push_str(&format!(
"## `{}` {{#function-library-{}}}\n\n",
title, ident
"## `{}` {{#{}{}}}\n\n",
title, anchor_prefix, ident
));

// <subtitle> (type signature)
Expand Down Expand Up @@ -193,8 +199,8 @@ impl ManualEntry {
// examples, how can this be achieved automatically?
if let Some(example) = &self.example {
output.push_str(&format!(
"::: {{.example #function-library-example-{}}}\n",
ident
"::: {{.example #{}example-{}}}\n",
anchor_prefix, ident
));
output.push_str(&format!("# `{}` usage example\n\n", title));
output.push_str(&format!("```nix\n{}\n```\n:::\n\n", example.trim()));
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ struct Options {
#[arg(short, long, default_value_t = String::from("lib"))]
prefix: String,

#[arg(long, default_value_t = String::from("function-library-"))]
anchor_prefix: String,

/// Whether to output JSON.
#[arg(short, long, default_value_t = false)]
json_output: bool,
Expand Down Expand Up @@ -310,6 +313,9 @@ fn collect_entries(
}

fn retrieve_description(nix: &rnix::Root, description: &str, category: &str) -> String {
if description.is_empty() && category.is_empty() {
return String::new();
}
format!(
"# {} {{#sec-functions-library-{}}}\n{}\n",
description,
Expand Down Expand Up @@ -351,7 +357,7 @@ fn main_with_options(opts: Options) -> String {
let mut output = description + "\n";

for entry in entries {
entry.write_section(&mut output);
entry.write_section(opts.anchor_prefix.as_str(), &mut output);
}
output
}
Expand Down
Loading