Skip to content

Commit

Permalink
Add cmark to processor
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Jul 8, 2024
1 parent 513d9e1 commit 00b0dc3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
2 changes: 2 additions & 0 deletions book/mdbook-trunk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ version.workspace = true
[dependencies]
clap = { version = "4.5.8", features = ["derive"] }
mdbook = "0.4.40"
pulldown-cmark = "0.11.0"
pulldown-cmark-to-cmark = "15.0.1"
semver = "1.0.23"
serde_json = "1.0.120"
91 changes: 83 additions & 8 deletions book/mdbook-trunk/src/preprocessor.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use mdbook::{
book::{Book, Chapter},
errors::Error,
preprocess::{Preprocessor, PreprocessorContext},
BookItem,
};
use pulldown_cmark::{Event, Parser};
use pulldown_cmark_to_cmark::cmark;

pub struct TrunkPreprocessor;

Expand All @@ -12,7 +13,25 @@ impl TrunkPreprocessor {
Self
}

fn process_chapter(&self, _chapter: &mut Chapter) {}
fn process_chapter(&self, chapter: &mut Chapter) -> Result<(), mdbook::errors::Error> {
let mut buf = String::with_capacity(chapter.content.len());

let events = Parser::new(&chapter.content).map(|event| {
println!("{:?}", event);

if let Event::Text(_text) = &event {}

event
});

if let Err(err) = cmark(events, &mut buf) {
Err(err.into())
} else {
chapter.content = buf;

Ok(())
}
}
}

impl Default for TrunkPreprocessor {
Expand All @@ -26,14 +45,14 @@ impl Preprocessor for TrunkPreprocessor {
"trunk"
}

fn run(&self, _ctx: &PreprocessorContext, book: Book) -> Result<Book, Error> {
fn run(&self, _ctx: &PreprocessorContext, book: Book) -> Result<Book, mdbook::errors::Error> {
let mut book = book.clone();

book.for_each_mut(|section| match section {
BookItem::Chapter(chapter) => self.process_chapter(chapter),
BookItem::Separator => {}
BookItem::PartTitle(_) => {}
});
for section in &mut book.sections {
if let BookItem::Chapter(chapter) = section {
self.process_chapter(chapter)?;
}
}

Ok(book)
}
Expand All @@ -42,3 +61,59 @@ impl Preprocessor for TrunkPreprocessor {
renderer == "html"
}
}

#[cfg(test)]
mod test {
use mdbook::preprocess::CmdPreprocessor;

use super::*;

#[test]
fn test_run() {
let input_json = r##"[
{
"root": "/path/to/book",
"config": {
"book": {
"authors": ["AUTHOR"],
"language": "en",
"multilingual": false,
"src": "src",
"title": "TITLE"
},
"preprocessor": {
"nop": {}
}
},
"renderer": "html",
"mdbook_version": "0.4.21"
},
{
"sections": [
{
"Chapter": {
"name": "Chapter 1",
"content": "# Chapter 1\n{{#trunk example.rs}}\n",
"number": [1],
"sub_items": [],
"path": "chapter_1.md",
"source_path": "chapter_1.md",
"parent_names": []
}
}
],
"__non_exhaustive": null
}
]"##
.as_bytes();

let (ctx, book) = CmdPreprocessor::parse_input(input_json).unwrap();
let expected_book = book.clone();

let result = TrunkPreprocessor::new().run(&ctx, book);
assert!(result.is_ok());

let actual_book = result.unwrap();
assert_eq!(actual_book, expected_book);
}
}

0 comments on commit 00b0dc3

Please sign in to comment.