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.
This commit is contained in:
Bram Gotink 2023-07-31 23:52:00 +02:00
parent ab2cb71c00
commit 18cb9091ea
No known key found for this signature in database
GPG Key ID: E76F2A54A5DA1CEA
3 changed files with 10 additions and 1 deletions

View File

@ -138,7 +138,8 @@ mod nop_lib {
"sub_items": [], "sub_items": [],
"path": "chapter_1.md", "path": "chapter_1.md",
"source_path": "chapter_1.md", "source_path": "chapter_1.md",
"parent_names": [] "parent_names": [],
"data": {}
} }
} }
], ],

View File

@ -165,6 +165,9 @@ pub struct Chapter {
pub source_path: Option<PathBuf>, pub source_path: Option<PathBuf>,
/// An ordered list of the names of each chapter above this one in the hierarchy. /// An ordered list of the names of each chapter above this one in the hierarchy.
pub parent_names: Vec<String>, 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 { impl Chapter {
@ -444,6 +447,7 @@ And here is some \
source_path: Some(PathBuf::from("second.md")), source_path: Some(PathBuf::from("second.md")),
parent_names: vec![String::from("Chapter 1")], parent_names: vec![String::from("Chapter 1")],
sub_items: Vec::new(), sub_items: Vec::new(),
data: serde_json::Map::new(),
}; };
let should_be = BookItem::Chapter(Chapter { let should_be = BookItem::Chapter(Chapter {
name: String::from("Chapter 1"), name: String::from("Chapter 1"),
@ -457,6 +461,7 @@ And here is some \
BookItem::Separator, BookItem::Separator,
BookItem::Chapter(nested), BookItem::Chapter(nested),
], ],
data: serde_json::Map::new(),
}); });
let got = load_summary_item(&SummaryItem::Link(root), temp.path(), Vec::new()).unwrap(); let got = load_summary_item(&SummaryItem::Link(root), temp.path(), Vec::new()).unwrap();
@ -533,6 +538,7 @@ And here is some \
Vec::new(), Vec::new(),
)), )),
], ],
data: serde_json::Map::new(),
}), }),
BookItem::Separator, BookItem::Separator,
], ],
@ -586,6 +592,7 @@ And here is some \
Vec::new(), Vec::new(),
)), )),
], ],
data: serde_json::Map::new(),
}), }),
BookItem::Separator, BookItem::Separator,
], ],

View File

@ -105,6 +105,7 @@ impl HtmlHandlebars {
ctx.data ctx.data
.insert("section".to_owned(), json!(section.to_string())); .insert("section".to_owned(), json!(section.to_string()));
} }
ctx.data.insert("data".to_owned(), json!(ch.data));
// Render the handlebars template with the data // Render the handlebars template with the data
debug!("Render template"); debug!("Render template");