Skip to content

Commit

Permalink
Add data property to chapter struct
Browse files Browse the repository at this point in the history
This data property makes it possible to share data
between pre-processors, or between pre-processors
and the renderer, including the "html" renderer
which exposes the chapter data as "data" in the
.hbs files.
  • Loading branch information
bgotink committed Aug 2, 2023
1 parent ab2cb71 commit 18cb909
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/nop-preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ mod nop_lib {
"sub_items": [],
"path": "chapter_1.md",
"source_path": "chapter_1.md",
"parent_names": []
"parent_names": [],
"data": {}
}
}
],
Expand Down
7 changes: 7 additions & 0 deletions src/book/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ pub struct Chapter {
pub source_path: Option<PathBuf>,
/// An ordered list of the names of each chapter above this one in the hierarchy.
pub parent_names: Vec<String>,
/// Data that can be shared between preprocessors, accessed in the renderer
/// or accessed in the theme's `.hbs` files
pub data: serde_json::Map<String, serde_json::Value>,
}

impl Chapter {
Expand Down Expand Up @@ -444,6 +447,7 @@ And here is some \
source_path: Some(PathBuf::from("second.md")),
parent_names: vec![String::from("Chapter 1")],
sub_items: Vec::new(),
data: serde_json::Map::new(),
};
let should_be = BookItem::Chapter(Chapter {
name: String::from("Chapter 1"),
Expand All @@ -457,6 +461,7 @@ And here is some \
BookItem::Separator,
BookItem::Chapter(nested),
],
data: serde_json::Map::new(),
});

let got = load_summary_item(&SummaryItem::Link(root), temp.path(), Vec::new()).unwrap();
Expand Down Expand Up @@ -533,6 +538,7 @@ And here is some \
Vec::new(),
)),
],
data: serde_json::Map::new(),
}),
BookItem::Separator,
],
Expand Down Expand Up @@ -586,6 +592,7 @@ And here is some \
Vec::new(),
)),
],
data: serde_json::Map::new(),
}),
BookItem::Separator,
],
Expand Down
1 change: 1 addition & 0 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl HtmlHandlebars {
ctx.data
.insert("section".to_owned(), json!(section.to_string()));
}
ctx.data.insert("data".to_owned(), json!(ch.data));

// Render the handlebars template with the data
debug!("Render template");
Expand Down

0 comments on commit 18cb909

Please sign in to comment.