Skip to content

Commit 18cb909

Browse files
committed
Add data property to chapter struct
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.
1 parent ab2cb71 commit 18cb909

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

examples/nop-preprocessor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ mod nop_lib {
138138
"sub_items": [],
139139
"path": "chapter_1.md",
140140
"source_path": "chapter_1.md",
141-
"parent_names": []
141+
"parent_names": [],
142+
"data": {}
142143
}
143144
}
144145
],

src/book/book.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ pub struct Chapter {
165165
pub source_path: Option<PathBuf>,
166166
/// An ordered list of the names of each chapter above this one in the hierarchy.
167167
pub parent_names: Vec<String>,
168+
/// Data that can be shared between preprocessors, accessed in the renderer
169+
/// or accessed in the theme's `.hbs` files
170+
pub data: serde_json::Map<String, serde_json::Value>,
168171
}
169172

170173
impl Chapter {
@@ -444,6 +447,7 @@ And here is some \
444447
source_path: Some(PathBuf::from("second.md")),
445448
parent_names: vec![String::from("Chapter 1")],
446449
sub_items: Vec::new(),
450+
data: serde_json::Map::new(),
447451
};
448452
let should_be = BookItem::Chapter(Chapter {
449453
name: String::from("Chapter 1"),
@@ -457,6 +461,7 @@ And here is some \
457461
BookItem::Separator,
458462
BookItem::Chapter(nested),
459463
],
464+
data: serde_json::Map::new(),
460465
});
461466

462467
let got = load_summary_item(&SummaryItem::Link(root), temp.path(), Vec::new()).unwrap();
@@ -533,6 +538,7 @@ And here is some \
533538
Vec::new(),
534539
)),
535540
],
541+
data: serde_json::Map::new(),
536542
}),
537543
BookItem::Separator,
538544
],
@@ -586,6 +592,7 @@ And here is some \
586592
Vec::new(),
587593
)),
588594
],
595+
data: serde_json::Map::new(),
589596
}),
590597
BookItem::Separator,
591598
],

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ impl HtmlHandlebars {
105105
ctx.data
106106
.insert("section".to_owned(), json!(section.to_string()));
107107
}
108+
ctx.data.insert("data".to_owned(), json!(ch.data));
108109

109110
// Render the handlebars template with the data
110111
debug!("Render template");

0 commit comments

Comments
 (0)