mdbook_obsidian/
lib.rs

1use mdbook::book::Book;
2use mdbook::book::{BookItem, Chapter};
3use mdbook::errors::Error;
4use mdbook::preprocess::{Preprocessor, PreprocessorContext};
5use rust_embed::RustEmbed;
6
7mod callouts;
8
9#[derive(RustEmbed)]
10#[folder = "assets/"]
11pub struct Asset;
12
13/// The Obsidian preprocessor.
14pub struct Obsidian;
15
16impl Default for Obsidian {
17    fn default() -> Self {
18        Self::new()
19    }
20}
21
22impl Obsidian {
23    pub fn new() -> Obsidian {
24        Obsidian
25    }
26}
27
28impl Preprocessor for Obsidian {
29    fn name(&self) -> &str {
30        "Obsidian"
31    }
32
33    fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> {
34        let mut error: Option<Error> = None;
35        book.for_each_mut(|item: &mut BookItem| {
36            if error.is_some() {
37                return;
38            }
39            if let BookItem::Chapter(ref mut chapter) = *item {
40                if let Err(err) = handle_chapter(chapter) {
41                    error = Some(err)
42                }
43            }
44        });
45        error.map_or(Ok(book), Err)
46    }
47
48    /// Check whether we support the specified renderer
49    fn supports_renderer(&self, renderer: &str) -> bool {
50        renderer == "html"
51    }
52}
53
54/// Apply to all chapters
55fn handle_chapter(chapter: &mut Chapter) -> Result<(), Error> {
56    chapter.content = callouts::render(&chapter.content)?;
57    // Add your additional syntax parsing here
58
59    Ok(())
60}