-
Notifications
You must be signed in to change notification settings - Fork 0
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
og:image, preproccesses, first image and you #55
Comments
Askeed at rust-lang/mdbook: rust-lang/mdBook#1416 (comment) |
Refer to ChatGPT: |
Create a new Rust project You can create a new Rust project using cargo, the Rust package manager:
This command will create a new directory my_preprocessor with a basic Rust project structure.
Add mdbook as a dependency in your Cargo.toml: [dependencies]
mdbook = "0.4.12" # Use the version of mdbook you're working with
You will need to create a struct (let's call it MyPreprocessor) that implements the Preprocessor trait from the mdbook crate. Here's a skeleton of what that might look like: use mdbook::preprocess::{Preprocessor, PreprocessorContext, CmdPreprocessor};
use mdbook::book::{Book, BookItem};
use mdbook::errors::Result;
pub struct MyPreprocessor;
impl Preprocessor for MyPreprocessor {
fn name(&self) -> &str {
"my_preprocessor"
}
fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book> {
// This is where you would modify the book.
// As a simple example, this preprocessor will add a message to the start of every chapter.
book.for_each_mut(|section: &mut BookItem| {
if let BookItem::Chapter(chapter) = section {
let new_content = format!("<!-- Processed by MyPreprocessor -->\n{}", chapter.content);
chapter.content = new_content;
}
});
Ok(book)
}
fn supports_renderer(&self, renderer: &str) -> bool {
renderer != "not-supported-renderer"
}
} In the run method, you iterate over each section of the book. If the section is a chapter, you prepend a message to the content.
You also need a main function that mdbook can call. You can use the CmdPreprocessor struct from the mdbook crate to handle most of the details: fn main() -> Result<()> {
let (ctx, book) = CmdPreprocessor::parse_input(std::io::stdin())?;
let preprocessor = MyPreprocessor;
let processed_book = preprocessor.run(&ctx, book)?;
serde_json::to_writer(std::io::stdout(), &processed_book)?;
Ok(())
}
Use
In your book.toml, you can add your preprocessor to the preprocessor field: [preprocessor.my_preprocessor]
command = "path/to/your/preprocessor/binary" |
og:image, preproccesses, first image and you #55
ugh closed for now |
ChatGPT:
Handlebars og:image Setter
The text was updated successfully, but these errors were encountered: