Skip to content

Commit 6880df3

Browse files
committed
Add before_body_end handlebars partial
Add a partial just before the body closing tag to add custom html code. The use case is for adding javascript snippet that involves more than just importing external scripts, e.g. web analytics.
1 parent 89a2e39 commit 6880df3

File tree

5 files changed

+13
-0
lines changed

5 files changed

+13
-0
lines changed

guide/src/format/theme/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Here are the files you can override:
1414
- **_index.hbs_** is the handlebars template.
1515
- **_head.hbs_** is appended to the HTML `<head>` section.
1616
- **_header.hbs_** content is appended on top of every book page.
17+
- **_before_body_end.hbs_** content is appended before body closing tag.
1718
- **_css/_** contains the CSS files for styling the book.
1819
- **_css/chrome.css_** is for UI elements.
1920
- **_css/general.css_** is the base styles.

src/renderer/html_handlebars/hbs_renderer.rs

+3
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,9 @@ impl Renderer for HtmlHandlebars {
502502
debug!("Register the header handlebars template");
503503
handlebars.register_partial("header", String::from_utf8(theme.header.clone())?)?;
504504

505+
debug!("Register the before_body_end handlebars template");
506+
handlebars.register_partial("before_body_end", String::from_utf8(theme.before_body_end.clone())?)?;
507+
505508
debug!("Register handlebars helpers");
506509
self.register_hbs_helpers(&mut handlebars, &html_config);
507510

src/theme/before_body_end.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{!-- Put your before_body_end HTML text here --}}

src/theme/index.hbs

+2
Original file line numberDiff line numberDiff line change
@@ -308,5 +308,7 @@
308308
{{/if}}
309309
{{/if}}
310310

311+
{{> before_body_end}}
312+
311313
</body>
312314
</html>

src/theme/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub static INDEX: &[u8] = include_bytes!("index.hbs");
1717
pub static HEAD: &[u8] = include_bytes!("head.hbs");
1818
pub static REDIRECT: &[u8] = include_bytes!("redirect.hbs");
1919
pub static HEADER: &[u8] = include_bytes!("header.hbs");
20+
pub static BEFORE_BODY_END: &[u8] = include_bytes!("before_body_end.hbs");
2021
pub static CHROME_CSS: &[u8] = include_bytes!("css/chrome.css");
2122
pub static GENERAL_CSS: &[u8] = include_bytes!("css/general.css");
2223
pub static PRINT_CSS: &[u8] = include_bytes!("css/print.css");
@@ -50,6 +51,7 @@ pub struct Theme {
5051
pub head: Vec<u8>,
5152
pub redirect: Vec<u8>,
5253
pub header: Vec<u8>,
54+
pub before_body_end: Vec<u8>,
5355
pub chrome_css: Vec<u8>,
5456
pub general_css: Vec<u8>,
5557
pub print_css: Vec<u8>,
@@ -83,6 +85,7 @@ impl Theme {
8385
(theme_dir.join("head.hbs"), &mut theme.head),
8486
(theme_dir.join("redirect.hbs"), &mut theme.redirect),
8587
(theme_dir.join("header.hbs"), &mut theme.header),
88+
(theme_dir.join("before_body_end.hbs"), &mut theme.before_body_end),
8689
(theme_dir.join("book.js"), &mut theme.js),
8790
(theme_dir.join("css/chrome.css"), &mut theme.chrome_css),
8891
(theme_dir.join("css/general.css"), &mut theme.general_css),
@@ -149,6 +152,7 @@ impl Default for Theme {
149152
head: HEAD.to_owned(),
150153
redirect: REDIRECT.to_owned(),
151154
header: HEADER.to_owned(),
155+
before_body_end: BEFORE_BODY_END.to_owned(),
152156
chrome_css: CHROME_CSS.to_owned(),
153157
general_css: GENERAL_CSS.to_owned(),
154158
print_css: PRINT_CSS.to_owned(),
@@ -206,6 +210,7 @@ mod tests {
206210
"head.hbs",
207211
"redirect.hbs",
208212
"header.hbs",
213+
"before_body_end.hbs",
209214
"favicon.png",
210215
"favicon.svg",
211216
"css/chrome.css",
@@ -236,6 +241,7 @@ mod tests {
236241
head: Vec::new(),
237242
redirect: Vec::new(),
238243
header: Vec::new(),
244+
before_body_end: Vec::new(),
239245
chrome_css: Vec::new(),
240246
general_css: Vec::new(),
241247
print_css: Vec::new(),

0 commit comments

Comments
 (0)