Skip to content

Commit e825357

Browse files
vabc3Michael-F-Bryan
authored andcommitted
Add option to disable section label in html (#533)
1 parent fd8f3bb commit e825357

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

book-example/src/format/config.md

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ The following configuration options are available:
8484
removing the current behaviour, you can specify a set of javascript files
8585
that will be loaded alongside the default one.
8686
- **playpen:** A subtable for configuring various playpen settings.
87+
- **no-section-label**: mdBook by defaults adds section label in table of
88+
contents column. For example, "1.", "2.1". Set this option to true to
89+
disable those labels. Defaults to `false`.
8790

8891
**book.toml**
8992
```toml

src/config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ pub struct HtmlConfig {
326326
/// This config item *should not be edited* by the end user.
327327
#[doc(hidden)]
328328
pub livereload_url: Option<String>,
329+
pub no_section_label: bool,
329330
}
330331

331332
/// Configuration for tweaking how the the HTML renderer handles the playpen.

src/renderer/html_handlebars/hbs_renderer.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ impl HtmlHandlebars {
239239
json!(utils::fs::path_to_root(Path::new("print.md"))));
240240
}
241241

242-
fn register_hbs_helpers(&self, handlebars: &mut Handlebars) {
243-
handlebars.register_helper("toc", Box::new(helpers::toc::RenderToc));
242+
fn register_hbs_helpers(&self, handlebars: &mut Handlebars, html_config: &HtmlConfig) {
243+
handlebars.register_helper("toc", Box::new(helpers::toc::RenderToc {no_section_label: html_config.no_section_label}));
244244
handlebars.register_helper("previous", Box::new(helpers::navigation::previous));
245245
handlebars.register_helper("next", Box::new(helpers::navigation::next));
246246
}
@@ -307,7 +307,7 @@ impl Renderer for HtmlHandlebars {
307307
)?;
308308

309309
debug!("[*]: Register handlebars helpers");
310-
self.register_hbs_helpers(&mut handlebars);
310+
self.register_hbs_helpers(&mut handlebars, &html_config);
311311

312312
let mut data = make_data(&ctx.root, &book, &ctx.config, &html_config)?;
313313

src/renderer/html_handlebars/helpers/toc.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use pulldown_cmark::{html, Event, Parser, Tag};
77

88
// Handlebars helper to construct TOC
99
#[derive(Clone, Copy)]
10-
pub struct RenderToc;
10+
pub struct RenderToc {
11+
pub no_section_label: bool
12+
}
1113

1214
impl HelperDef for RenderToc {
1315
fn call(&self, _h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
@@ -91,11 +93,13 @@ impl HelperDef for RenderToc {
9193
false
9294
};
9395

94-
// Section does not necessarily exist
95-
if let Some(section) = item.get("section") {
96-
rc.writer.write_all(b"<strong>")?;
97-
rc.writer.write_all(section.as_bytes())?;
98-
rc.writer.write_all(b"</strong> ")?;
96+
if !self.no_section_label {
97+
// Section does not necessarily exist
98+
if let Some(section) = item.get("section") {
99+
rc.writer.write_all(b"<strong>")?;
100+
rc.writer.write_all(section.as_bytes())?;
101+
rc.writer.write_all(b"</strong> ")?;
102+
}
99103
}
100104

101105
if let Some(name) = item.get("name") {

0 commit comments

Comments
 (0)