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

og:image, preproccesses, first image and you #55

Closed
joelouthan opened this issue Jun 17, 2023 · 4 comments
Closed

og:image, preproccesses, first image and you #55

joelouthan opened this issue Jun 17, 2023 · 4 comments

Comments

@joelouthan
Copy link
Owner

ChatGPT: Handlebars og:image Setter

@joelouthan
Copy link
Owner Author

Askeed at rust-lang/mdbook: rust-lang/mdBook#1416 (comment)

@joelouthan
Copy link
Owner Author

Refer to ChatGPT: Image Loading Issues

@joelouthan
Copy link
Owner Author

Create a new Rust project

You can create a new Rust project using cargo, the Rust package manager:

arduino

cargo new my_preprocessor

This command will create a new directory my_preprocessor with a basic Rust project structure.

  1. Add dependencies

Add mdbook as a dependency in your Cargo.toml:

[dependencies]
mdbook = "0.4.12"  # Use the version of mdbook you're working with
  1. Implement the Preprocessor trait

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.

  1. Create a main function to interface with mdbook

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(())
}
  1. Build your preprocessor

Use cargo build --release to build your preprocessor. The resulting binary will be in the target/release directory.

  1. Add your preprocessor to your book

In your book.toml, you can add your preprocessor to the preprocessor field:

[preprocessor.my_preprocessor]
command = "path/to/your/preprocessor/binary"

joelouthan added a commit that referenced this issue Jun 28, 2023
og:image, preproccesses, first image and you #55
@joelouthan
Copy link
Owner Author

ugh closed for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant