Skip to content

Commit

Permalink
Add basic scarb doc documentation (#1518)
Browse files Browse the repository at this point in the history
  • Loading branch information
wawel37 authored Aug 13, 2024
1 parent f165a16 commit dd8a567
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
4 changes: 4 additions & 0 deletions website/.vitepress/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ const sidebar = {
},
p("Language server", "/docs/extensions/cairo-language-server"),
p("Expand", "/docs/extensions/expand"),
p(
"Generating documentation",
"/docs/extensions/documentation-generation",
),
],
},
{
Expand Down
66 changes: 66 additions & 0 deletions website/docs/extensions/documentation-generation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Generating documentation

`scarb doc` is a tool that generates documentation based on the code comments. Generation supports different output formats. The result is being placed inside the `/target/doc`.

## Supported output formats

- Markdown. Fully supported by [mdBook](https://rust-lang.github.io/mdBook/). (Default)
- Custom JSON

## Available types of comments

As for now, only the `///` comment prefix is supported.

## mdBook

Generated markdown can be used to create a documentation book.
Requirements:

- Install [mdBook](https://rust-lang.github.io/mdBook/guide/installation.html) by running `cargo install mdbook`.
- Run `scarb doc` inside the project root.
- Run `mdbook build` (or `mdbook serve`) inside the generated documentation target (`/target/doc/<PACKAGE-NAME>`).

## Examples

Let's take, for example, a simple Cairo project initalized using `scarb new`. Let's change the code inside `lib.cairo` to:

````cairo
/// Example Enum.
enum ExampleEnum {
/// First enum variant.
VARIANT_A,
/// Second enum variant.
VARIANT_B
}
/// Example struct. Contains a public field and a private one.
struct ExampleStruct {
/// Private field.
field_a: felt252,
/// Public field.
pub field_b: felt252
}
/// Function that prints "test" to stdout with endline.
/// Can invoke it like that:
/// ```cairo
/// fn main() {
/// test();
/// }
/// ```
fn test() {
println!("test");
}
/// Main function that Cairo runs as a binary entrypoint.
fn main() {
println!("hello_world");
}
````

After running `scarb doc`, inside the target directory, you will see the generated documentation in `mdBook` format which consists of:

- The `src` directory, which contains the contents of your book in files with markdown format.
- The `book.toml` which contains contains settings for describing how to build your book.

Running `scarb doc --output-format json` will result in a single JSON file inside the target directory with collected documentation inside.

0 comments on commit dd8a567

Please sign in to comment.