From e825357848d7641e10abbd9ca4bff0ff17cc73c6 Mon Sep 17 00:00:00 2001 From: vabc3 Date: Mon, 8 Jan 2018 00:31:46 +0800 Subject: [PATCH] Add option to disable section label in html (#533) --- book-example/src/format/config.md | 3 +++ src/config.rs | 1 + src/renderer/html_handlebars/hbs_renderer.rs | 6 +++--- src/renderer/html_handlebars/helpers/toc.rs | 16 ++++++++++------ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md index 786dce3c..c5048eae 100644 --- a/book-example/src/format/config.md +++ b/book-example/src/format/config.md @@ -84,6 +84,9 @@ The following configuration options are available: removing the current behaviour, you can specify a set of javascript files that will be loaded alongside the default one. - **playpen:** A subtable for configuring various playpen settings. +- **no-section-label**: mdBook by defaults adds section label in table of + contents column. For example, "1.", "2.1". Set this option to true to + disable those labels. Defaults to `false`. **book.toml** ```toml diff --git a/src/config.rs b/src/config.rs index b83293e2..3216255e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -326,6 +326,7 @@ pub struct HtmlConfig { /// This config item *should not be edited* by the end user. #[doc(hidden)] pub livereload_url: Option, + pub no_section_label: bool, } /// Configuration for tweaking how the the HTML renderer handles the playpen. diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index ac256eaf..694186ee 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -239,8 +239,8 @@ impl HtmlHandlebars { json!(utils::fs::path_to_root(Path::new("print.md")))); } - fn register_hbs_helpers(&self, handlebars: &mut Handlebars) { - handlebars.register_helper("toc", Box::new(helpers::toc::RenderToc)); + fn register_hbs_helpers(&self, handlebars: &mut Handlebars, html_config: &HtmlConfig) { + handlebars.register_helper("toc", Box::new(helpers::toc::RenderToc {no_section_label: html_config.no_section_label})); handlebars.register_helper("previous", Box::new(helpers::navigation::previous)); handlebars.register_helper("next", Box::new(helpers::navigation::next)); } @@ -307,7 +307,7 @@ impl Renderer for HtmlHandlebars { )?; debug!("[*]: Register handlebars helpers"); - self.register_hbs_helpers(&mut handlebars); + self.register_hbs_helpers(&mut handlebars, &html_config); let mut data = make_data(&ctx.root, &book, &ctx.config, &html_config)?; diff --git a/src/renderer/html_handlebars/helpers/toc.rs b/src/renderer/html_handlebars/helpers/toc.rs index 0c633bad..1cc971de 100644 --- a/src/renderer/html_handlebars/helpers/toc.rs +++ b/src/renderer/html_handlebars/helpers/toc.rs @@ -7,7 +7,9 @@ use pulldown_cmark::{html, Event, Parser, Tag}; // Handlebars helper to construct TOC #[derive(Clone, Copy)] -pub struct RenderToc; +pub struct RenderToc { + pub no_section_label: bool +} impl HelperDef for RenderToc { fn call(&self, _h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { @@ -91,11 +93,13 @@ impl HelperDef for RenderToc { false }; - // Section does not necessarily exist - if let Some(section) = item.get("section") { - rc.writer.write_all(b"")?; - rc.writer.write_all(section.as_bytes())?; - rc.writer.write_all(b" ")?; + if !self.no_section_label { + // Section does not necessarily exist + if let Some(section) = item.get("section") { + rc.writer.write_all(b"")?; + rc.writer.write_all(section.as_bytes())?; + rc.writer.write_all(b" ")?; + } } if let Some(name) = item.get("name") {