mdbook-trunk 0.0.2

mdBook plugin which bundles packages using Trunk and includes them as iframes.
Documentation
use std::str;

use anyhow::Result;
use log::debug;
use mdbook::{
    book::Book,
    preprocess::{Preprocessor, PreprocessorContext},
    BookItem,
};

use crate::{parser::code_block::parse_code_blocks, trunk::iframe};

pub struct TrunkPreprocessor;

impl TrunkPreprocessor {
    pub fn new() -> Self {
        Self
    }
}

impl Default for TrunkPreprocessor {
    fn default() -> Self {
        Self::new()
    }
}

impl Preprocessor for TrunkPreprocessor {
    fn name(&self) -> &str {
        "trunk"
    }

    fn run(&self, _ctx: &PreprocessorContext, book: Book) -> Result<Book> {
        debug!("Trunk preprocessor {:?}", book);

        let mut book = book.clone();

        for section in &mut book.sections {
            if let BookItem::Chapter(chapter) = section {
                let blocks = parse_code_blocks(chapter)?;
                for (span, config) in blocks {
                    chapter.content.replace_range(span, &iframe(&config)?);
                }
            }
        }

        Ok(book)
    }

    fn supports_renderer(&self, _renderer: &str) -> bool {
        true
    }
}