Add option to disable section label in html (#533)

This commit is contained in:
vabc3 2018-01-08 00:31:46 +08:00 committed by Michael Bryan
parent fd8f3bb415
commit e825357848
4 changed files with 17 additions and 9 deletions

View File

@ -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

View File

@ -326,6 +326,7 @@ pub struct HtmlConfig {
/// This config item *should not be edited* by the end user.
#[doc(hidden)]
pub livereload_url: Option<String>,
pub no_section_label: bool,
}
/// Configuration for tweaking how the the HTML renderer handles the playpen.

View File

@ -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)?;

View File

@ -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,12 +93,14 @@ impl HelperDef for RenderToc {
false
};
if !self.no_section_label {
// Section does not necessarily exist
if let Some(section) = item.get("section") {
rc.writer.write_all(b"<strong>")?;
rc.writer.write_all(section.as_bytes())?;
rc.writer.write_all(b"</strong> ")?;
}
}
if let Some(name) = item.get("name") {
// Render only inline code blocks