| Crates.io | mdbook-include-rs |
| lib.rs | mdbook-include-rs |
| version | 0.1.0 |
| created_at | 2025-03-14 23:32:47.010559+00 |
| updated_at | 2025-03-14 23:32:47.010559+00 |
| description | An mdBook preprocessor that includes external Rust source files |
| homepage | |
| repository | https://github.com/bryncooke/mdbook-include-rs |
| max_upload_size | |
| id | 1592851 |
| size | 125,242 |
A preprocessor for mdBook that lets you include code from external Rust source files with extraction capabilities. This makes it easy to maintain code examples that are always in sync with your actual codebase.
First, install the preprocessor:
cargo install mdbook-include-rs
Then, modify your book.toml file:
[book]
title = "My Book"
authors = ["Your Name"]
# Add the preprocessor to your mdBook
[preprocessor.include-rs]
# Optional: Specify a base directory for all file paths
# If not provided, paths will be relative to each markdown file's directory
# [preprocessor.include-rs]
# base-dir = "examples"
To include an entire source file:
```rust
#![source_file!("source_file.rs")]
```
This will include the entire contents of the file, with use statements automatically filtered out for cleaner output.
To include just the body of a specific function (without the function declaration):
```rust
#![function_body!("source_file.rs", hello_world)]
```
The output will only contain the content inside the function body, not the function declaration itself.
If your function depends on other types or functions, you can include them too:
```rust
#![function_body!("src/models.rs", User::display_profile, [struct User, trait Displayable, impl Displayable for User])]
```
This will include:
User struct definitionDisplayable trait definitionDisplayable for Userdisplay_profile methodThe dependencies will be included in the order you list them, with the main function's body appearing last.
You can include various types of dependencies:
struct StructName - includes a struct definitiontrait TraitName - includes a trait definitionimpl StructName - includes all methods in an impl block for a structimpl StructName::method_name - includes a specific method from an impl blockimpl TraitName for StructName - includes a trait implementationfunction_name - includes another functionFor a document explaining user authentication:
# User Authentication
Our system handles authentication with a simple API:
```rust
{{#function_body!("src/auth.rs", authenticate_user, [struct Credentials, struct User, fn validate_password])}}
```
Behind the scenes, password hashing works like this:
```rust
{{#function_body!("src/auth.rs", hash_password)}}
```
The preprocessor:
include-doc directivessyn library to parse and extract the requested code elementsAs per the mdBook preprocessor standard:
# Check if the preprocessor supports a renderer
mdbook-include-doc supports <renderer>
# Process a book
mdbook-include-doc pre-process <path-to-book>
This project is licensed under: